一、简单介绍
简单来说,音频可以分为2种
(1)音效
又称“短音频”,通常在程序中的播放时长为1~2秒
在应用程序中起到点缀效果,提升整体用户体验
(2)音乐
比如游戏中的“背景音乐”,一般播放时间较长
框架:播放音频需要用到AVFoundation.framework框架
二、音效的播放
1.获得音效文件的路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加载音效文件,得到对应的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
注意:音效文件只需要加载1次
4.音效播放常见函数总结
加载音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
释放音效资源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效带点震动
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
三、程序示例
先导入需要依赖的框架
说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。
导入需要播放的音效文件素材
代码示例:
ViewController.m
1 // 2 // ViewController.m 3 // 01-音效播放 4 // 5 // Created by apple on 14/11/7. 6 // Copyright (c) 2014年 github. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <AVFoundation/AVFoundation.h> 11 #import "HMAudioTool.h" 12 13 @interface ViewController () 14 @property (nonatomic, assign) SystemSoundID soundID; 15 16 17 @end 18 19 @implementation ViewController 20 21 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 22 { 23 NSLog(@"%s", __func__); 24 25 /* 26 // -1.创建URL 27 NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil]; 28 29 // 0.创建音效ID 30 SystemSoundID soundID; 31 AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); 32 33 // 1.播放音效(本地音效) 34 #warning ios8的模拟器不支持播放音效(真机可以) 35 AudioServicesPlaySystemSound(soundID); 36 */ 37 38 // 1.播放音效(本地音效) 39 // AudioServicesPlaySystemSound(self.soundID); 40 41 // 利用工具类播放音效 42 // [HMAudioTool playAudioWithFilename:@"buyao.wav"]; 43 44 45 // 随机播放 46 // NSString *filename = [NSString stringWithFormat:@"m_%02d.wav", arc4random_uniform(14) + 3]; 47 // [HMAudioTool playAudioWithFilename:filename]; 48 49 // 同一时刻播放多个音效 50 // [HMAudioTool playAudioWithFilename:@"m_17.wav"]; 51 // [HMAudioTool playAudioWithFilename:@"buyao.wav"]; 52 53 // 利用AudioServicesPlaySystemSound播放音乐, 但是注意: 真实开发中不建议使用该函数播放音乐 54 [HMAudioTool playAudioWithFilename:@"normal.aac"]; 55 56 } 57 58 // 接收到内存警告 59 - (void)didReceiveMemoryWarning 60 { 61 [HMAudioTool disposeAudioWithFilename:@"buyao.wav"]; 62 } 63 64 /* 65 - (SystemSoundID)soundID 66 { 67 if (!_soundID) { 68 // -1.创建URL 69 NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil]; 70 71 // 0.创建音效ID 72 AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &_soundID); 73 } 74 return _soundID; 75 } 76 */ 77 @end
自定义音频工具类
HMAudioTool.h
1 // 2 // HMAudioTool.h 3 // 01-音效播放 4 // 5 // Created by apple on 14/11/7. 6 // Copyright (c) 2014年 github. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface HMAudioTool : NSObject 12 // 播放音效 13 // 传入需要 播放的音效文件名称 14 + (void)playAudioWithFilename:(NSString *)filename; 15 16 // 销毁音效 17 + (void)disposeAudioWithFilename:(NSString *)filename; 18 @end
HMAudioTool.m
1 // 2 // HMAudioTool.m 3 // 01-音效播放 4 // 5 // Created by apple on 14/11/7. 6 // Copyright (c) 2014年 github. All rights reserved. 7 // 8 9 #import "HMAudioTool.h" 10 #import <AVFoundation/AVFoundation.h> 11 12 @implementation HMAudioTool 13 14 static NSMutableDictionary *_soundIDs; 15 /* 16 + (void)initialize 17 { 18 _soundIDs = [NSMutableDictionary dictionary]; 19 } 20 */ 21 22 + (NSMutableDictionary *)soundIDs 23 { 24 if (!_soundIDs) { 25 _soundIDs = [NSMutableDictionary dictionary]; 26 } 27 return _soundIDs; 28 } 29 30 + (void)playAudioWithFilename:(NSString *)filename 31 { 32 /* 33 // -1.创建URL 34 NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 35 36 // 0.创建音效ID 37 SystemSoundID soundID; 38 AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); 39 40 // 1.播放音效(本地音效) 41 #warning ios8的模拟器不支持播放音效(真机可以) 42 AudioServicesPlaySystemSound(soundID); 43 */ 44 // 0.判断文件名是否为nil 45 if (filename == nil) { 46 return; 47 } 48 49 // 1.从字典中取出音效ID 50 SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue]; 51 52 // 判断音效ID是否为nil 53 if (!soundID) { 54 NSLog(@"创建新的soundID"); 55 56 // 音效ID为nil 57 // 根据文件名称加载音效URL 58 NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 59 60 // 判断url是否为nil 61 if (!url) { 62 return; 63 } 64 65 // 创建音效ID 66 AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); 67 68 // 将音效ID添加到字典中 69 [self soundIDs][filename] = @(soundID); 70 } 71 // 播放音效 72 AudioServicesPlaySystemSound(soundID); 73 } 74 75 + (void)disposeAudioWithFilename:(NSString *)filename 76 { 77 // 0.判断文件名是否为nil 78 if (filename == nil) { 79 return; 80 } 81 82 // 1.从字典中取出音效ID 83 SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue]; 84 85 if (soundID) { 86 // 2.销毁音效ID 87 AudioServicesDisposeSystemSoundID(soundID); 88 89 // 3.从字典中移除已经销毁的音效ID 90 [[self soundIDs] removeObjectForKey:filename]; 91 } 92 93 } 94 @end
说明:点击屏幕可以播放音效文件。
来源:https://www.cnblogs.com/zengshuilin/p/5768231.html