iOS调用系统音频

来源:互联网 发布:js监听手机端软键盘 编辑:程序博客网 时间:2024/06/05 00:23

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

那么如何做呢?

两步解决

1.导入#import<AudioToolbox/AudioToolbox.h>2.在调用音频的地方写如下代码//定义一个SystemSoundIDSystemSoundID soundID = 1108;//具体参数详情下面贴出来//播放声音AudioServicesPlaySystemSound(soundID);

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

播放自定义音频

        - (void)playNotifySound {//获取路径NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];//定义一个SystemSoundIDSystemSoundID 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);
0 0
原创粉丝点击