• Swift


    1,系统声音服务介绍:

    系统声音服务提供了一个Api,用于播放不超过30秒的声音。它支持的文件格式有限,具体的说只有CAF、AIF和使用PCM或IMA/ADPCM数据的WAV文件。
    但此函数没有提供操作声音和控制音量的功能,因此如果是要为多媒体或游戏创建专门声音,就不要使用系统声音服务。

    2,系统声音服务支持如下三种类型:
    (1)声音:立刻播放一个简单的声音文件。如果手机静音,则用户什么也听不见。
    (2)提醒:播放一个声音文件,如果手机设为静音或震动,则通过震动提醒用户。
    (3)震动:震动手机,而不考虑其他设置。

    3,使用样例(首先类中要引入AudioToolbox)
    1
    import AudioToolbox

    (1)声音播放

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @IBAction func systemSound(sender: AnyObject) {
        //建立的SystemSoundID对象
        var soundID:SystemSoundID = 0
        //获取声音地址
        var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")
        //地址转换
        var baseURL = NSURL(fileURLWithPath: path!)
        //赋值
        AudioServicesCreateSystemSoundID(baseURL, &soundID)
        //播放声音
        AudioServicesPlaySystemSound(soundID)
    }


    (2)提醒

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @IBAction func systemAlert(sender: AnyObject) {
        //建立的SystemSoundID对象
        var soundID:SystemSoundID = 0
        //获取声音地址
        var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")
        //地址转换
        var baseURL = NSURL(fileURLWithPath: path!)
        //赋值
        AudioServicesCreateSystemSoundID(baseURL, &soundID)
        //提醒(同上面唯一的一个区别)
        AudioServicesPlayAlertSound(soundID)
    }


    (3)振动

    1
    2
    3
    4
    5
    6
    @IBAction func systemVibration(sender: AnyObject) {
        //建立的SystemSoundID对象
        var soundID = SystemSoundID(kSystemSoundID_Vibrate)
        //振动
        AudioServicesPlaySystemSound(soundID)
    }
  • 相关阅读:
    Windows server 2008 R2充当路由器实现网络的互联(转)
    sqlserver内存释放心得
    收藏一个好用的base64编解码网址
    最后一篇,说好的开源来了!
    python五子棋
    flask使用原生ajax、不使用表单(Form)上传文件
    使用keras的LSTM进行预测----实战练习
    keras神经网络三个例子
    【强化学习】用pandas 与 numpy 分别实现 q-learning, saras, saras(lambda)算法
    【转】【强化学习】Deep Q Network(DQN)算法详解
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4843381.html
Copyright © 2020-2023  润新知