标签:
1 #import "ViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 @interface ViewController () 4 @property (nonatomic,assign) SystemSoundID soundID; 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 } 13 14 //需要懒加载一下 -否则没声音 会报err-50错误 15 -(SystemSoundID)soundID 16 { 17 if (!_soundID) 18 { 19 NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil]; 20 AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &_soundID); 21 } 22 return _soundID; 23 } 24 25 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 26 { 27 //加载音效文件(短音频) 1个音效文件-对应1个SoundID 28 // SystemSoundID soundID; 29 // NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil]; 30 // AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)url, &soundID); 31 32 //拿到音效ID播放 33 AudioServicesPlaySystemSound(self.soundID); 34 35 } 36 37 -(void)didReceiveMemoryWarning 38 { 39 [super didReceiveMemoryWarning]; 40 41 AudioServicesDisposeSystemSoundID(self.soundID);// 有创建就有销毁 42 self.soundID = 0; 43 }
但是由于 每个SoundID对应一个音频 所以当有多个音频时 就得繁琐的弄多个SoundID, 为了简化,创建一个工具类
AudioTool
+(void)playSound:(NSString *)fileName
{
SystemSoundID soundID;
NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)url, &soundID);
}
//由于销毁时 需拿到以前创建的soundID来销毁,因此要将那个SoundID拿出来作为全局的(此刻或许会想到成员变量-但是类方法是不允许访问成员变量的,因此弄一个全局的static)
+(void)disposeSound:(NSString *)fileName
{
}
1 #import "AudioTool.h" 2 #import <AVFoundation/AVFoundation.h> 3 @implementation AudioTool 4 5 // 字典: filename作为key, soundID作为value 6 // 存放所有的音频ID 7 static NSMutableDictionary *_soundIDDict; 8 +(void)initialize 9 { 10 _soundIDDict = [NSMutableDictionary dictionary]; 11 } 12 13 +(void)playSound:(NSString *)fileName 14 { 15 if (!fileName) return; 16 17 SystemSoundID soundID = [_soundIDDict[fileName] unsignedIntValue]; 18 if (!soundID)//创建 19 { 20 NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil]; 21 if (!url) return; 22 23 //1----创建音效id 24 AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)url, &soundID); 25 26 //放入字典 27 _soundIDDict[fileName] = @(soundID); 28 } 29 30 // 2-----播放--AudioServicesPlaySystemSound播放短音频 长音频-AudioPlayer 31 AudioServicesPlaySystemSound(soundID); 32 } 33 34 +(void)disposeSound:(NSString *)fileName 35 { 36 if (!fileName) return; 37 SystemSoundID soundID = [_soundIDDict[fileName] unsignedIntValue]; 38 if (soundID)//若有soundID 销毁 39 { 40 AudioServicesDisposeSystemSoundID(soundID); 41 42 [_soundIDDict removeObjectForKey:fileName];//从字典中移除 43 } 44 }
1 #import "ViewController.h" 2 //#import <AVFoundation/AVFoundation.h> 3 #import "AudioTool.h" 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 } 13 14 15 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 16 { 17 NSString *filename = [NSString stringWithFormat:@"m_%02d.wav",arc4random_uniform(14)+3]; 18 19 [AudioTool playSound:filename]; 20 } 21 22 -(void)didReceiveMemoryWarning 23 { 24 [super didReceiveMemoryWarning]; 25 26 // [AudioTool disposeSound:@"m_03.wav"]; 27 }
标签:
原文地址:http://www.cnblogs.com/doudajun520/p/5159536.html