我对音频技术不是很精通,最近对这方面比较有兴趣,公司以前的项目用到这方面的技术不多。我只是粗略的涉猎没有深入研究。今天就研究一下写一篇博客。同时希望以后能做一个关于音频的商业项目也期望能接触到音频开发技术的大牛。
随着移动互联网的发展,智能手机功能越来越丰富。播放音乐、视频、录音、拍照等都是很常用的功能。在iOS中对于多媒体的支持是非常强大的,今天的文章中将会对音频进行介绍。
在iOS中音频播放从形式上可以分为音效播放和音乐播放。前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度、循环等控制。后者指的是一些较长的音频,通常是主音频,对于这些音频的播放通常需要进行精确的控制。在iOS中播放两类音频分别使用AudioToolbox.framework和AVFoundation.framework来完成音效和音乐播放。
音效
这里我先介绍我做的一个项目,app名为“叮叮收银”这是一个与收银相关的项目。当时要做的效果是当收银成功有一个“叮”的音效。这样比较人性化,当时产品说会和老板讨论确定这个短音效。我当时就先考虑了短音频的播放。不过后来我发现其实系统声音完全可以。我当时听了好多个系统声音最后确定了一个大家都比较满意的。不过测试声音时要连接真机测试,模拟器是不可以的。
下面是简单示例:
1.导入#import
2.播放时调用AudioServicesPlaySystemSound(SystemSoundID);
当参数为 1000-2000 之间数字时就是播放系统声音(我的项目用的是1054音效)
系统声音对照表:
Sound ID | File name (iPhone) | File name (iPod Touch) | Category | Note |
---|---|---|---|---|
1000 | new-mail.caf | new-mail.caf | MailReceived | |
1001 | mail-sent.caf | mail-sent.caf | MailSent | |
1002 | Voicemail.caf | Voicemail.caf | VoicemailReceived | |
1003 | ReceivedMessage.caf | ReceivedMessage.caf | SMSReceived | |
1004 | SentMessage.caf | SentMessage.caf | SMSSent | |
1005 | alarm.caf | sq_alarm.caf | CalendarAlert | |
1006 | low_power.caf | low_power.caf | LowPower | |
1007 | sms-received1.caf | sms-received1.caf | SMSReceived_Alert | |
1008 | sms-received2.caf | sms-received2.caf | SMSReceived_Alert | |
1009 | sms-received3.caf | sms-received3.caf | SMSReceived_Alert | |
1010 | sms-received4.caf | sms-received4.caf | SMSReceived_Alert | |
1011 | - | - | SMSReceived_Vibrate | |
1012 | sms-received1.caf | sms-received1.caf | SMSReceived_Alert | |
1013 | sms-received5.caf | sms-received5.caf | SMSReceived_Alert | |
1014 | sms-received6.caf | sms-received6.caf | SMSReceived_Alert | |
1015 | Voicemail.caf | Voicemail.caf | - | Available since 2.1 |
1016 | tweet_sent.caf | tweet_sent.caf | SMSSent | Available since 5.0 |
1020 | Anticipate.caf | Anticipate.caf | SMSReceived_Alert | Available since 4.2 |
1021 | Bloom.caf | Bloom.caf | SMSReceived_Alert | Available since 4.2 |
1022 | Calypso.caf | Calypso.caf | SMSReceived_Alert | Available since 4.2 |
1023 | Choo_Choo.caf | Choo_Choo.caf | SMSReceived_Alert | Available since 4.2 |
1024 | Descent.caf | Descent.caf | SMSReceived_Alert | Available since 4.2 |
1025 | Fanfare.caf | Fanfare.caf | SMSReceived_Alert | Available since 4.2 |
1026 | Ladder.caf | Ladder.caf | SMSReceived_Alert | Available since 4.2 |
1027 | Minuet.caf | Minuet.caf | SMSReceived_Alert | Available since 4.2 |
1028 | News_Flash.caf | News_Flash.caf | SMSReceived_Alert | Available since 4.2 |
1029 | Noir.caf | Noir.caf | SMSReceived_Alert | Available since 4.2 |
1030 | Sherwood_Forest.caf | Sherwood_Forest.caf | SMSReceived_Alert | Available since 4.2 |
1031 | Spell.caf | Spell.caf | SMSReceived_Alert | Available since 4.2 |
1032 | Suspense.caf | Suspense.caf | SMSReceived_Alert | Available since 4.2 |
1033 | Telegraph.caf | Telegraph.caf | SMSReceived_Alert | Available since 4.2 |
1034 | Tiptoes.caf | Tiptoes.caf | SMSReceived_Alert | Available since 4.2 |
1035 | Typewriters.caf | Typewriters.caf | SMSReceived_Alert | Available since 4.2 |
1036 | Update.caf | Update.caf | SMSReceived_Alert | Available since 4.2 |
1050 | ussd.caf | ussd.caf | USSDAlert | |
1051 | SIMToolkitCallDropped.caf | SIMToolkitCallDropped.caf | SIMToolkitTone | |
1052 | SIMToolkitGeneralBeep.caf | SIMToolkitGeneralBeep.caf | SIMToolkitTone | |
1053 | SIMToolkitNegativeACK.caf | SIMToolkitNegativeACK.caf | SIMToolkitTone | |
1054 | SIMToolkitPositiveACK.caf | SIMToolkitPositiveACK.caf | SIMToolkitTone | |
1055 | SIMToolkitSMS.caf | SIMToolkitSMS.caf | SIMToolkitTone | |
1057 | Tink.caf | Tink.caf | PINKeyPressed | |
1070 | ct-busy.caf | ct-busy.caf | AudioToneBusy | There was no category for this sound before 4.0. |
1071 | ct-congestion.caf | ct-congestion.caf | AudioToneCongestion | There was no category for this sound before 4.0. |
1072 | ct-path-ack.caf | ct-path-ack.caf | AudioTonePathAcknowledge | There was no category for this sound before 4.0. |
1073 | ct-error.caf | ct-error.caf | AudioToneError | There was no category for this sound before 4.0. |
1074 | ct-call-waiting.caf | ct-call-waiting.caf | AudioToneCallWaiting | There was no category for this sound before 4.0. |
1075 | ct-keytone2.caf | ct-keytone2.caf | AudioToneKey2 | There was no category for this sound before 4.0. |
1100 | lock.caf | sq_lock.caf | ScreenLocked | |
1101 | unlock.caf | sq_lock.caf | ScreenUnlocked | |
1102 | - | - | FailedUnlock | |
1103 | Tink.caf | sq_tock.caf | KeyPressed | |
1104 | Tock.caf | sq_tock.caf | KeyPressed | |
1105 | Tock.caf | sq_tock.caf | KeyPressed | |
1106 | beep-beep.caf | sq_beep-beep.caf | ConnectedToPower | |
1107 | RingerChanged.caf | RingerChanged.caf | RingerSwitchIndication | |
1108 | photoShutter.caf | photoShutter.caf | CameraShutter | |
1109 | shake.caf | shake.caf | ShakeToShuffle | Available since 3.0 |
1110 | jbl_begin.caf | jbl_begin.caf | JBL_Begin | Available since 3.0 |
1111 | jbl_confirm.caf | jbl_confirm.caf | JBL_Confirm | Available since 3.0 |
1112 | jbl_cancel.caf | jbl_cancel.caf | JBL_Cancel | Available since 3.0 |
1113 | begin_record.caf | begin_record.caf | BeginRecording | Available since 3.0 |
1114 | end_record.caf | end_record.caf | EndRecording | Available since 3.0 |
1115 | jbl_ambiguous.caf | jbl_ambiguous.caf | JBL_Ambiguous | Available since 3.0 |
1116 | jbl_no_match.caf | jbl_no_match.caf | JBL_NoMatch | Available since 3.0 |
1117 | begin_video_record.caf | begin_video_record.caf | BeginVideoRecording | Available since 3.0 |
1118 | end_video_record.caf | end_video_record.caf | EndVideoRecording | Available since 3.0 |
1150 | vc~invitation-accepted.caf | vc~invitation-accepted.caf | VCInvitationAccepted | Available since 4.0 |
1151 | vc~ringing.caf | vc~ringing.caf | VCRinging | Available since 4.0 |
1152 | vc~ended.caf | vc~ended.caf | VCEnded | Available since 4.0 |
1153 | ct-call-waiting.caf | ct-call-waiting.caf | VCCallWaiting | Available since 4.1 |
1154 | vc~ringing.caf | vc~ringing.caf | VCCallUpgrade | Available since 4.1 |
1200 | dtmf-0.caf | dtmf-0.caf | TouchTone | |
1201 | dtmf-1.caf | dtmf-1.caf | TouchTone | |
1202 | dtmf-2.caf | dtmf-2.caf | TouchTone | |
1203 | dtmf-3.caf | dtmf-3.caf | TouchTone | |
1204 | dtmf-4.caf | dtmf-4.caf | TouchTone | |
1205 | dtmf-5.caf | dtmf-5.caf | TouchTone | |
1206 | dtmf-6.caf | dtmf-6.caf | TouchTone | |
1207 | dtmf-7.caf | dtmf-7.caf | TouchTone | |
1208 | dtmf-8.caf | dtmf-8.caf | TouchTone | |
1209 | dtmf-9.caf | dtmf-9.caf | TouchTone | |
1210 | dtmf-star.caf | dtmf-star.caf | TouchTone | |
1211 | dtmf-pound.caf | dtmf-pound.caf | TouchTone | |
1254 | long_low_short_high.caf | long_low_short_high.caf | Headset_StartCall | |
1255 | short_double_high.caf | short_double_high.caf | Headset_Redial | |
1256 | short_low_high.caf | short_low_high.caf | Headset_AnswerCall | |
1257 | short_double_low.caf | short_double_low.caf | Headset_EndCall | |
1258 | short_double_low.caf | short_double_low.caf | Headset_CallWaitingActions | |
1259 | middle_9_short_double_low.caf | middle_9_short_double_low.caf | Headset_TransitionEnd | |
1300 | Voicemail.caf | Voicemail.caf | SystemSoundPreview | |
1301 | ReceivedMessage.caf | ReceivedMessage.caf | SystemSoundPreview | |
1302 | new-mail.caf | new-mail.caf | SystemSoundPreview | |
1303 | mail-sent.caf | mail-sent.caf | SystemSoundPreview | |
1304 | alarm.caf | sq_alarm.caf | SystemSoundPreview | |
1305 | lock.caf | sq_lock.caf | SystemSoundPreview | |
1306 | Tock.caf | sq_tock.caf | KeyPressClickPreview | The category was SystemSoundPreview before 3.2. |
1307 | sms-received1.caf | sms-received1.caf | SMSReceived_Selection | |
1308 | sms-received2.caf | sms-received2.caf | SMSReceived_Selection | |
1309 | sms-received3.caf | sms-received3.caf | SMSReceived_Selection | |
1310 | sms-received4.caf | sms-received4.caf | SMSReceived_Selection | |
1311 | - | - | SMSReceived_Vibrate | |
1312 | sms-received1.caf | sms-received1.caf | SMSReceived_Selection | |
1313 | sms-received5.caf | sms-received5.caf | SMSReceived_Selection | |
1314 | sms-received6.caf | sms-received6.caf | SMSReceived_Selection | |
1315 | Voicemail.caf | Voicemail.caf | SystemSoundPreview | Available since 2.1 |
1320 | Anticipate.caf | Anticipate.caf | SMSReceived_Selection | Available since 4.2 |
1321 | Bloom.caf | Bloom.caf | SMSReceived_Selection | Available since 4.2 |
1322 | Calypso.caf | Calypso.caf | SMSReceived_Selection | Available since 4.2 |
1323 | Choo_Choo.caf | Choo_Choo.caf | SMSReceived_Selection | Available since 4.2 |
1324 | Descent.caf | Descent.caf | SMSReceived_Selection | Available since 4.2 |
1325 | Fanfare.caf | Fanfare.caf | SMSReceived_Selection | Available since 4.2 |
1326 | Ladder.caf | Ladder.caf | SMSReceived_Selection | Available since 4.2 |
1327 | Minuet.caf | Minuet.caf | SMSReceived_Selection | Available since 4.2 |
1328 | News_Flash.caf | News_Flash.caf | SMSReceived_Selection | Available since 4.2 |
1329 | Noir.caf | Noir.caf | SMSReceived_Selection | Available since 4.2 |
1330 | Sherwood_Forest.caf | Sherwood_Forest.caf | SMSReceived_Selection | Available since 4.2 |
1331 | Spell.caf | Spell.caf | SMSReceived_Selection | Available since 4.2 |
1332 | Suspense.caf | Suspense.caf | SMSReceived_Selection | Available since 4.2 |
1333 | Telegraph.caf | Telegraph.caf | SMSReceived_Selection | Available since 4.2 |
1334 | Tiptoes.caf | Tiptoes.caf | SMSReceived_Selection | Available since 4.2 |
1335 | Typewriters.caf | Typewriters.caf | SMSReceived_Selection | Available since 4.2 |
1336 | Update.caf | Update.caf | SMSReceived_Selection | Available since 4.2 |
1350 | - | - | RingerVibeChanged | |
1351 | - | - | SilentVibeChanged | |
4095 | - | - | Vibrate | There was no category for this sound before 2.2. In the SDK this is the constant kSystemSoundID_Vibrate. |
AudioToolbox.framework是一套基于C语言的框架,使用它来播放音效其本质是将短音频注册到系统声音服务(System Sound Service)。System Sound Service是一种简单、底层的声音播放服务,但是它本身也存在着一些限制:
1. 音频播放时间不能超过30s
2.数据必须是PCM或者IMA4格式
3.音频文件必须打包成.caf、.aif、.wav中的一种(注意这是官方文档的说法,实际测试发现一些.mp3也可以播放)
使用System Sound Service 播放音效的步骤如下:
1.调用AudioServicesCreateSystemSoundID( CFURLRef inFileURL, SystemSoundID* outSystemSoundID)函数获得系统声音ID。
2.如果需要监听播放完成操作,则使用AudioServicesAddSystemSoundCompletion( SystemSoundID inSystemSoundID,CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioServicesSystemSoundCompletionProc inCompletionRoutine, void* inClientData)方法注册回调函数。
3.调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效(后者带有震动效果)。】
下面是简单示例:
1.导入#import
- (void)viewDidLoad{
[super viewDidLoad];
[self playSoundEffect:@"feedtip.wav"];
}
/**
* 播放完成回调函数
*
* @param soundID 系统声音ID
* @param clientData 回调时传递的数据
*/
void soundCompleteCallback(SystemSoundID soundID,void * clientData){
NSLog(@"播放完成...");
}
/**
* 播放音效文件
*
* @param name 音频文件名称
*/
-(void)playSoundEffect:(NSString *)name{
NSString *audioFile=[[NSBundle mainBundle] pathForResource:name ofType:nil];
NSURL *fileUrl=[NSURL fileURLWithPath:audioFile];
//1.获得系统声音ID
SystemSoundID soundID = 0;
/**
* inFileUrl:音频文件url
* outSystemSoundID:声音id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID)
*/
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
//如果需要在播放完之后执行某些操作,可以调用如下方法注册一个播放完成回调函数
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
//2.播放音频
AudioServicesPlaySystemSound(soundID);//播放音效
// AudioServicesPlayAlertSound(soundID);//播放音效并震动
}
音乐
如果播放较大的音频或者要对音频有精确的控制则System Sound Service可能就很难满足实际需求了,通常这种情况会选择使用AVFoundation.framework中的AVAudioPlayer来实现。AVAudioPlayer可以看成一个播放器,它支持多种音频格式,而且能够进行进度、音量、播放速度等控制。首先简单看一下AVAudioPlayer常用的属性和方法:
属性 | 说明 |
@property(readonly, getter=isPlaying) BOOL playing | 是否正在播放,只读 |
@property(readonly) NSUInteger numberOfChannels | 音频声道数,只读 |
@property(readonly) NSTimeInterval duration | 音频时长 |
@property(readonly) NSURL *url | 音频文件路径,只读 |
@property(readonly) NSData *data | 音频数据,只读 |
@property float pan | 立体声平衡,如果为-1.0则完全左声道,如果0.0则左右声道平衡,如果为1.0则完全为右声道 |
@property float volume | 音量大小,范围0-1.0 |
@property BOOL enableRate | 是否允许改变播放速率 |
@property float rate | 播放速率,范围0.5-2.0,如果为1.0则正常播放,如果要修改播放速率则必须设置enableRate为YES |
@property NSTimeInterval currentTime | 当前播放时长 |
@property(readonly) NSTimeInterval deviceCurrentTime | 输出设备播放音频的时间,注意如果播放中被暂停此时间也会继续累加 |
@property NSInteger numberOfLoops | 循环播放次数,如果为0则不循环,如果小于0则无限循环,大于0则表示循环次数 |
@property(readonly) NSDictionary *settings | 音频播放设置信息,只读 |
@property(getter=isMeteringEnabled) BOOL meteringEnabled | 是否启用音频测量,默认为NO,一旦启用音频测量可以通过updateMeters方法更新测量值 |
对象方法 | 说明 |
- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError | 使用文件URL初始化播放器,注意这个URL不能是HTTP URL,AVAudioPlayer不支持加载网络媒体流,只能播放本地文件 |
- (instancetype)initWithData:(NSData *)data error:(NSError **)outError | 使用NSData初始化播放器,注意使用此方法时必须文件格式和文件后缀一致,否则出错,所以相比此方法更推荐使用上述方法或- (instancetype)initWithData:(NSData *)data fileTypeHint:(NSString *)utiString error:(NSError **)outError方法进行初始化 |
- (BOOL)prepareToPlay; | 加载音频文件到缓冲区,注意即使在播放之前音频文件没有加载到缓冲区程序也会隐式调用此方法。 |
- (BOOL)play; | 播放音频文件 |
- (BOOL)playAtTime:(NSTimeInterval)time | 在指定的时间开始播放音频 |
- (void)pause; | 暂停播放 |
- (void)stop; | 停止播放 |
- (void)updateMeters | 更新音频测量值,注意如果要更新音频测量值必须设置meteringEnabled为YES,通过音频测量值可以即时获得音频分贝等信息 |
- (float)peakPowerForChannel:(NSUInteger)channelNumber; | 获得指定声道的分贝峰值,注意如果要获得分贝峰值必须在此之前调用updateMeters方法 |
- (float)averagePowerForChannel:(NSUInteger)channelNumber | 获得指定声道的分贝平均值,注意如果要获得分贝平均值必须在此之前调用updateMeters方法 |
@property(nonatomic, copy) NSArray *channelAssignments | 获得或设置播放声道 |
代理方法 | 说明 |
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag | 音频播放完成 |
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error | 音频解码发生错误 |
AVAudioPlayer的使用比较简单:
1.导入#import
今天有事先更到这里。
参考链接:http://www.cnblogs.com/kenshincui/p/4186022.html