iOS高级程序师技术群:622177838,期待你的加入
音效的播放
1.获得音效文件的路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加载音效文件,得到对应的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
- (void)playSound:(NSString *)soundName
{
SystemSoundID soundID;
NSURL* sample = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:soundName ofType:@"m4a"]];
OSStatus err = AudioServicesCreateSystemSoundID((__bridge CFURLRef)(sample), &soundID);
if (err)
{
NSLog(@"Error occurred assigning system sound!");
return;
}
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, SoundFinished,(__bridge void *)(sample));
AudioServicesPlaySystemSound(soundID);
CFRunLoopRun();
}