文章規(guī)劃
iOS 音視頻開(kāi)發(fā)(一)常用音視頻框架介紹
iOS 音視頻開(kāi)發(fā)(二)AVAudioRecorder實(shí)現(xiàn)錄音功能(本篇)
iOS 音視頻開(kāi)發(fā)(三)MediaPlayer播放本地、遠(yuǎn)程音頻
iOS 音視頻開(kāi)發(fā)(四)MediaPlayer播放本地、遠(yuǎn)程視頻
iOS 音視頻開(kāi)發(fā)(五)AVAudioPlayer/AVPlayer播放本地、遠(yuǎn)程音頻
AVAudioRecorder、AVAudioPlayer 屬于AVFoundation框架,使用時(shí)需要先導(dǎo)入<AVFoundation/AVFoundation.h>框架頭文件。
AVFoundation
是蘋(píng)果的現(xiàn)代媒體框架,它包含了一些不同用途的 API 和不同層級(jí)的抽象。其中有一些是Objective-C 對(duì)于底層 C 語(yǔ)言接口的封裝。除了少數(shù)的例外情況,AVFoundation 可以同時(shí)在 iOS 和 mac OS ?中使用。
AVAudioRecorder
錄音機(jī),提供了在應(yīng)用程序中的音頻記錄能力。作為與 AVAudioPlayer 相對(duì)應(yīng)的 API,AVAudioRecorder 是將音頻錄制為文件的最簡(jiǎn)單的方法。除了用一個(gè)音量計(jì)接受音量的峰值和平均值以外,這個(gè) API 簡(jiǎn)單粗暴,如果你的使用場(chǎng)景很簡(jiǎn)單的話,這可能恰恰就是你想要的方法。
AVAudioPlayer
這個(gè)高層級(jí)的 API 為你提供一個(gè)簡(jiǎn)單的接口,用來(lái)播放本地或者內(nèi)存中的音頻。這是一個(gè)無(wú)界面的音頻播放器 (也就是說(shuō)沒(méi)有提供 UI 元素),使用起來(lái)也很直接簡(jiǎn)單。它不適用于網(wǎng)絡(luò)音頻流或者低延遲的實(shí)時(shí)音頻播放。如果這些問(wèn)題都不需要擔(dān)心,那么 AVAudioPlayer 可能就是正確的選擇。音頻播放器的 API 也為我們帶來(lái)了一些額外的功能,比如循環(huán)播放、獲取音頻的音量強(qiáng)度等等。
本文中Demo下載及截圖:
AVAudioRecorder錄音使用介紹:
1、導(dǎo)入AVFoundation框架
#import<AVFoundation/AVFoundation.h>
2、獲取沙盒路徑
- (NSString*)filePath {
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString*filePath = [path stringByAppendingPathComponent:@"voice.caf"];
returnfilePath;
}
3、錄音會(huì)話設(shè)置(小編試了一下,如果不設(shè)置錄音會(huì)話,播放錄音的聲音會(huì)很?。?/b>
NSError*errorSession =nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];//得到AVAudioSession單例對(duì)象
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &errorSession];//設(shè)置類(lèi)別,表示該應(yīng)用同時(shí)支持播放和錄音
[audioSession setActive:YES error: &errorSession];//啟動(dòng)音頻會(huì)話管理,此時(shí)會(huì)阻斷后臺(tái)音樂(lè)的播放.
//設(shè)置成揚(yáng)聲器播放
UInt32doChangeDefault =1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefault), &doChangeDefault);
4、創(chuàng)建錄音配置信息的字典
NSDictionary*setting =@{
AVFormatIDKey:@(kAudioFormatAppleIMA4),//音頻格式
AVSampleRateKey:@44100.0f,//錄音采樣率(Hz)如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
AVNumberOfChannelsKey:@1,//音頻通道數(shù)1或2
AVEncoderBitDepthHintKey:@16,//線性音頻的位深度8、16、24、32
AVEncoderAudioQualityKey:@(AVAudioQualityHigh)//錄音的質(zhì)量
};
5、創(chuàng)建存放錄音文件的地址(音頻流寫(xiě)入文件的本地文件URL)
NSURL*url = [NSURL URLWithString:[self filePath]];
6、初始化AVAudioRecorder對(duì)象
NSError*error;
self.audioRecorder= [[AVAudioRecorder alloc] initWithURL:url settings:setting error:&error];
if(self.audioRecorder) {
? ?self.audioRecorder.delegate=self;
? ?self.audioRecorder.meteringEnabled=YES;
? ?//設(shè)置錄音時(shí)長(zhǎng),超過(guò)這個(gè)時(shí)間后,會(huì)暫停單位是秒
? ?[self.audioRecorder recordForDuration:30];
? ?//創(chuàng)建一個(gè)音頻文件,并準(zhǔn)備系統(tǒng)進(jìn)行錄制
? ?[self.audioRecorder prepareToRecord];
} else {
? ?NSLog(@"Error: %@", [error localizedDescription]);
}
7、開(kāi)始錄音
[self.audioRecorder record];//開(kāi)始錄音(或者暫停后,繼續(xù)錄音)
[self.audioRecorder pause];//暫停錄音
[self.audioRecorder stop];//停止錄制并關(guān)閉音頻文件
8、常用 AVAudioRecorderDelegate
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:(BOOL)flag;
AVAudioPlayer播放錄音文件:
1、初始化AVAudioPlayer對(duì)象
NSError*error;
self.player= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfile URLWithPath:[self filePath]] error:&error];
//設(shè)置播放循環(huán)次數(shù)
[self.player setNumberOfLoops:0];
[self.player setVolume:1];//音量,0-1之間
//分配播放所需的資源,并將其加入內(nèi)部播放隊(duì)列
[self.player setDelegate:self];
[self.player prepareToPlay];
2、播放錄音
[self.player play];//播放錄音
[self.player pause];//暫停播放錄音
[self.player stop];//停止播放錄音
3、常用AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag;
4、AVAudioPlayer播放本地音頻文件的代碼在Demo中。