使用AVAudioRecorder 錄音

來源:互聯網
上載者:User


http://www.cnblogs.com/chinaxxren/archive/2013/01/16/2863483.html


自己項目中用到了錄音部分,本來想做一個關於錄音的開源項目的,想想還是寫部落格好點。

錄音回話設定

1  NSError *error = nil;2     AVAudioSession * audioSession = [AVAudioSession sharedInstance]; //得到AVAudioSession單例對象3     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];//設定類別,表示該應用同時支援播放和錄音4     [audioSession setActive:YES error: &error];//啟動音頻會話管理,此時會阻斷後台音樂的播放.5     6     // 設定成擴音器播放7     UInt32 doChangeDefault = 1;8     AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefault), &doChangeDefault);

1-4主要設定程式啟動錄音狀態,7-8設定的是預設為採用擴音器狀態播放,如果你插入耳機,自動改變為耳機狀態。

錄音之前需要,先關閉播放音樂,如果不關閉,就開始錄音,就會出現紅色提示框,造成整個view抖動。不過不影響使用。所以在使用上面的代碼之前先調用代碼關閉音樂播放。

1  NSMutableDictionary *recordSetting = [NSMutableDictionary dictionary];2     [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];3     [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];4     [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];5     [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];6     [recordSetting setValue:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsBigEndianKey];7     [recordSetting setValue:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsFloatKey];

我錄音的格式是ma4格式,採用這種格式的主要目的是,此格式錄音一分鐘產生的大小小於1M,剛好滿足蘋果官方的審核。這裡單聲道和雙聲道大小還是有有一點區別的,但不太多。我非專業人士,聽不出來好壞。你也可以錄製完畢轉換成mp3格式。github上有一個開源的項目 https://github.com/rpplusplus/iOSMp3Recorder 錄製的各個參數我就不再分析。

接下來就是初始化,

1  NSError *error = nil;2  AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:_recordFileURL settings:recordSettings error:&error];
_recordFileURL 是NSURL的檔案路徑。
recordSettings 是上面的音頻格式設定。

設定錄音時間。得到輸入大小。完成委託。

1 [_recorder recordForDuration:(NSTimeInterval) 30.0 * 60.0];2  _recorder.meteringEnabled = YES;3 [_recorder setDelegate:self];

1設定最長的錄音時間。2得到輸入大小,最大為60,最小為-60 。3設定設定委託,委託裡面有兩個方法,一個當錄音完成調用,一個當出現中斷調用。

1 if ([_recorder prepareToRecord] == YES){2         [_recorder record];3 }

當準備好了就開始錄音,千萬不要只調用 prepareToRecord 方法,也許你第一次能調用成功,第二次不能調用成功。

然後就是設定動態顯示的東西,如動態錄音時間,_recorder.currentTime;

得到波形圖。

1 - (void)updateMeters; /* call to refresh meter values */2 3 - (float)peakPowerForChannel:(NSUInteger)channelNumber; /* returns peak power in decibels for a given channel */4 - (float)averagePowerForChannel:(NSUInteger)channelNumber; /* returns average power in decibels for a given channel */

使用上面的方法得到每個聲道的輸入大小。每次都必須調用 updateMeters來更新為最新資料。

錄音部分,就完成了,當然你可以對應的使用AVAudioPlayer來播放錄製的聲音。

1  AVAudioSession *audioSession = [AVAudioSession sharedInstance];2  NSError *err = nil;3  [audioSession setCategory :AVAudioSessionCategoryPlayback error:&err];

錄製完畢後調用上面切換成播放模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.