01--音效 (短音频)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01--音效 (短音频)相关的知识,希望对你有一定的参考价值。

音频 分为两种:
1、音效
2、音乐
播放需要两个框架:
AVFoundation.framework
AudioToolbox.framework
 技术分享
技术分享
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 }
ViewController Code

 

但是由于 每个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 }
AudioTool

 

技术分享
 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 }
ViewController Code

 

以上是关于01--音效 (短音频)的主要内容,如果未能解决你的问题,请参考以下文章

iOS学习笔记23-音效与音乐

Unity3.5 导入音频文件

iOS 播放系统音效

iOS 播放系统音效

iOS开发 - 多媒体

短视频运营短视频剪辑 ⑤ ( 视频素材使用 | 设置插入后的视频素材属性 | 设置画面 | 设置音频 | 设置变速 | 设置动画 | 设置调节 )