iOS 播放音效

音效:通俗的说是短音频,音频文件必须满足下面的条件

  1、播放的时间不能超过30秒

  2、数据必须是 PCM或者IMA4流格式

  3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)

1.引入框架

 1 #import <AudioToolbox/AudioToolbox.h> 

 

2.播放音效

 1 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

 2 {

 3     // 声明SoundID

 4     SystemSoundID soundID ;

 5     self.soundID = soundID;

 6     // 获取音效文件的url

 7     NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_10.wav" withExtension:nil];

 8     // 根据url创建

 9     AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);

10     AudioServicesPlaySystemSound(soundID);

11 };

  记忆技巧:

  1.播放音效的核心代码,申明音效ID,然后播放音效

   1 SystemSoundID soundID ; 2 AudioServicesPlaySystemSound(soundID); 

  2.用下面这句给soundID赋值

   1 AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID); 

  3.赋值语句有2个参数,一个是音效文件资源路径,另外一个就是之前申明soundID了

   1 NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_10.wav" withExtension:nil]; 

 

3.播放完音效后,应该销毁

1 - (void)didReceiveMemoryWarning

2 {

3     [super didReceiveMemoryWarning];

4     AudioServicesDisposeSystemSoundID(self.soundID);

5     self.soundID = 0;

6 }

 


 

重构上述代码,提供一个播放音效的工具类,重构代码来自MJ老师

1.工具类的.h文件

 1 #import <Foundation/Foundation.h>

 2 

 3 @interface MJAudioTool : NSObject

 4 

 5 /**

 6  *  播放音效

 7  *

 8  *  @param filename 音效文件名

 9  */

10 + (void)playSound:(NSString *)filename;

11 

12 /**

13  *  销毁音效

14  *

15  *  @param filename 音效文件名

16  */

17 + (void)disposeSound:(NSString *)filename;

18 @end

2.工具类.m文件

 1 #import "MJAudioTool.h"

 2 #import <AudioToolbox/AudioToolbox.h>

 3 

 4 @implementation MJAudioTool

 5 

 6 // 字典: filename作为key, soundID作为value

 7 // 存放所有的音频ID

 8 static NSMutableDictionary *_soundIDDict;

 9 + (void)initialize

10 {

11     _soundIDDict = [NSMutableDictionary dictionary];

12 }

13 

14 + (void)playSound:(NSString *)filename

15 {

16     if (!filename) return;

17     

18     // 1.从字典中取出soundID

19     SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];

20     if (!soundID) { // 创建

21         // 加载音效文件

22         NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];

23         

24         if (!url) return;

25         

26         // 创建音效ID

27         AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);

28         

29         // 放入字典

30         _soundIDDict[filename] = @(soundID);

31     }

32     

33     // 2.播放

34     AudioServicesPlaySystemSound(soundID);

35 }

36 

37 + (void)disposeSound:(NSString *)filename

38 {

39     if (!filename) return;

40     

41     SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];

42     if (soundID) {

43         // 销毁音效ID

44         AudioServicesDisposeSystemSoundID(soundID);

45         

46         // 从字典中移除

47         [_soundIDDict removeObjectForKey:filename];

48     }

49 }

50 

51 @end

3.控制器的.m文件

播放音效

1 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

2 {

3     [MJAudioTool playSound:@"buyao.wav"];

4     [MJAudioTool playSound:@"m_03.wav"];

5 }

销毁音效

1 - (void)didReceiveMemoryWarning

2 {

3     [super didReceiveMemoryWarning];

4     

5     [MJAudioTool disposeSound:@"m_03.wav"];

6     [MJAudioTool disposeSound:@"buyao.wav"];

7 }

 

  

  

 

你可能感兴趣的:(ios)