本文章向大家介紹OC-音效播放,主要包括OC-音效播放使用實例、應用技巧、基本知識點總結和需要注意事項,只此記錄一下,以便后續用到。
直接上代碼:
- (void)playVoice {
//獲取路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"newmsg1.mp3" withExtension:nil];
//根據音效文件創建systemSoundId
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
//根據soundId播放音效一:
// AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
// NSLog(@"play finish");
// //當音效播放完成時,我們需要根據SystemSoundID 釋放內存資源
// AudioServicesDisposeSystemSoundID(soundID);
// });
// 根據soundId播放音效二:播放音效時,手機會跟著震動;
AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
AudioServicesDisposeSystemSoundID(soundID);
});
}