iOS调用系统音频

最近项目需求,要求调用系统拍照的声音

那么如何做呢?

两步解决
1.导入#import<AudioToolbox/AudioToolbox.h>
2.在调用音频的地方写如下代码

//定义一个SystemSoundID
SystemSoundID soundID = 1108;//具体参数详情下面贴出来
//播放声音
AudioServicesPlaySystemSound(soundID);

自带音频ID:http://iphonedevwiki.net/index.php/AudioServices

播放自定义音频

        - (void)playNotifySound {
//获取路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
//定义一个SystemSoundID
SystemSoundID soundID;
//判断路径是否存在
if (path) {
    //创建一个音频文件的播放系统声音服务器
    OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
    //判断是否有错误
    if (error != kAudioServicesNoError) {
        NSLog(@"%d",(int)error);
    }
}
//播放声音和振动
AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
    //播放成功回调
});

}

只有振动没有声音

     //手机只振动没声音
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值