最近项目需求,要求调用系统拍照的声音
那么如何做呢?
两步解决
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);