我的播放音频文件: 注意导入
#import <AudioToolbox/AudioToolbox.h>
- (BOOL)playSystomSound:(NSString*)filename
{
if (filename == nil)
return NO;
SystemSoundID shortSound =
0;
NSString *soundPath = [[NSBundle
mainBundle] pathForResource:filename
ofType:@"caf"];
// If this file is actually in the bundle...
NSFileManager *fileManager = [NSFileManager
defaultManager];
if ([fileManager fileExistsAtPath:soundPath]) {
// Create a file URL with this path
NSURL *soundURL = [NSURL
fileURLWithPath:soundPath];
// Register sound file located at that URL as a system sound
OSStatus err =
AudioServicesCreateSystemSoundID((CFURLRef)soundURL, //这个地方在上篇文章里应该还有个__bridge,但我的项目不是ARC,是手动管理 ,所以不需要)
&shortSound);
if (err !=
kAudioServicesNoError){
}
} else {
return NO;
}
AudioServicesAddSystemSoundCompletion(shortSound,
NULL, NULL, &SoundFinished,
NULL);
AudioServicesPlaySystemSound(shortSound);
return YES;
}
(不然会导致内存泄漏)
#pragma mark --
#pragma mark AudioServicesPlaySystemSound call back
void SoundFinished(SystemSoundID snd,void *context)
{
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}