音效的播放oop
1.得到音效文件的路徑url
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];spa
2.加載音效文件,獲得對應的音效ID3d
SystemSoundID soundID = 0;orm
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);it
3.播放音效io
AudioServicesPlaySystemSound(soundID);file
- (void)playSound:(NSString *)soundNamemargin
{top
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();
}