文章規(guī)劃
iOS 音視頻開發(fā)(二)AVAudioRecorder實現錄音功能(本篇)
iOS 音視頻開發(fā)(三)MediaPlayer播放本地寻狂、遠程音頻
iOS 音視頻開發(fā)(四)MediaPlayer播放本地祥款、遠程視頻
iOS 音視頻開發(fā)(五)AVAudioPlayer/AVPlayer播放本地持隧、遠程音頻
AVAudioRecorder、AVAudioPlayer 屬于AVFoundation框架恒序,使用時需要先導入<AVFoundation/AVFoundation.h>框架頭文件三圆。
AVFoundation
是蘋果的現代媒體框架,它包含了一些不同用途的 API 和不同層級的抽象污呼。其中有一些是Objective-C 對于底層 C 語言接口的封裝裕坊。除了少數的例外情況,AVFoundation 可以同時在 iOS 和 mac OS ?中使用燕酷。
AVAudioRecorder
錄音機籍凝,提供了在應用程序中的音頻記錄能力。作為與 AVAudioPlayer 相對應的 API苗缩,AVAudioRecorder 是將音頻錄制為文件的最簡單的方法饵蒂。除了用一個音量計接受音量的峰值和平均值以外,這個 API 簡單粗暴酱讶,如果你的使用場景很簡單的話退盯,這可能恰恰就是你想要的方法。
AVAudioPlayer
這個高層級的 API 為你提供一個簡單的接口泻肯,用來播放本地或者內存中的音頻渊迁。這是一個無界面的音頻播放器 (也就是說沒有提供 UI 元素),使用起來也很直接簡單灶挟。它不適用于網絡音頻流或者低延遲的實時音頻播放宫纬。如果這些問題都不需要擔心,那么 AVAudioPlayer 可能就是正確的選擇膏萧。音頻播放器的 API 也為我們帶來了一些額外的功能漓骚,比如循環(huán)播放、獲取音頻的音量強度等等榛泛。
本文中Demo下載及截圖:
AVAudioRecorder錄音使用介紹:
1蝌蹂、導入AVFoundation框架
#import<AVFoundation/AVFoundation.h>
2、獲取沙盒路徑
- (NSString*)filePath {
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString*filePath = [path stringByAppendingPathComponent:@"voice.caf"];
returnfilePath;
}
3曹锨、錄音會話設置(小編試了一下孤个,如果不設置錄音會話,播放錄音的聲音會很信婕颉)
NSError*errorSession =nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];//得到AVAudioSession單例對象
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &errorSession];//設置類別,表示該應用同時支持播放和錄音
[audioSession setActive:YES error: &errorSession];//啟動音頻會話管理,此時會阻斷后臺音樂的播放.
//設置成揚聲器播放
UInt32doChangeDefault =1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefault), &doChangeDefault);
4齐鲤、創(chuàng)建錄音配置信息的字典
NSDictionary*setting =@{
AVFormatIDKey:@(kAudioFormatAppleIMA4),//音頻格式
AVSampleRateKey:@44100.0f,//錄音采樣率(Hz)如:AVSampleRateKey==8000/44100/96000(影響音頻的質量)
AVNumberOfChannelsKey:@1,//音頻通道數1或2
AVEncoderBitDepthHintKey:@16,//線性音頻的位深度8斥废、16、24给郊、32
AVEncoderAudioQualityKey:@(AVAudioQualityHigh)//錄音的質量
};
5牡肉、創(chuàng)建存放錄音文件的地址(音頻流寫入文件的本地文件URL)
NSURL*url = [NSURL URLWithString:[self filePath]];
6、初始化AVAudioRecorder對象
NSError*error;
self.audioRecorder= [[AVAudioRecorder alloc] initWithURL:url settings:setting error:&error];
if(self.audioRecorder) {
? ?self.audioRecorder.delegate=self;
? ?self.audioRecorder.meteringEnabled=YES;
? ?//設置錄音時長淆九,超過這個時間后统锤,會暫停單位是秒
? ?[self.audioRecorder recordForDuration:30];
? ?//創(chuàng)建一個音頻文件,并準備系統(tǒng)進行錄制
? ?[self.audioRecorder prepareToRecord];
} else {
? ?NSLog(@"Error: %@", [error localizedDescription]);
}
7炭庙、開始錄音
[self.audioRecorder record];//開始錄音(或者暫停后饲窿,繼續(xù)錄音)
[self.audioRecorder pause];//暫停錄音
[self.audioRecorder stop];//停止錄制并關閉音頻文件
8、常用 AVAudioRecorderDelegate
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:(BOOL)flag;
AVAudioPlayer播放錄音文件:
1焕蹄、初始化AVAudioPlayer對象
NSError*error;
self.player= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfile URLWithPath:[self filePath]] error:&error];
//設置播放循環(huán)次數
[self.player setNumberOfLoops:0];
[self.player setVolume:1];//音量逾雄,0-1之間
//分配播放所需的資源,并將其加入內部播放隊列
[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中迹卢。