詳解IPhone之 AVAudioRecorder 代碼實現

來源:互聯網
上載者:User

詳解IPhoneAVAudioRecorder 代碼實現是本文要介紹的內容,內容不多,基本屬於代碼實現。我們來看詳細內容。

#import <AVFoundation/AVFoundation.h>  需要引入

 
  1. //擷取document目錄的路徑  
  2. - (NSString*) documentsPath {  
  3.  if (! _documentsPath) {  
  4.   NSArray *searchPaths =  
  5.    NSSearchPathForDirectoriesInDomains 
  6.    (NSDocumentDirectory, NSUserDomainMask, YES);  
  7.   _documentsPath = [searchPaths objectAtIndex: 0];  
  8.   [_documentsPath retain];  
  9.  }  
  10.  return _documentsPath;  
  11. }  
  12. //(document目錄的路徑)  
  13.  NSString *destinationString = [[self documentsPath]  
  14.    stringByAppendingPathComponent:filenameField.text];  
  15.  NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];  
  16.  
  17. //初始化AVAudioRecorder  
  18.  NSError *recorderSetupError = nil;  
  19.  AVAudioRecorder audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL  
  20.    settings:recordSettings error:&recorderSetupError];   
  21.  [recordSettings release]; 

第二個參數  settings是一個容納索引值對的NSDictionary有四種一般的鍵

1、一般的音頻設定

2、線性PCM設定

3、編碼器設定

4、採樣率轉換設定

 
  1. NSMutableDictionary  需要加入五個設定值(線性PCM)  
  2.  
  3. NSMutableDictionary *recordSettings =  
  4.   [[NSMutableDictionary alloc] initWithCapacity:10];  
  5.  
  6.   //1 ID號  
  7.   [recordSettings setObject:  
  8.    [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];  
  9.   float sampleRate =  
  10.    [pcmSettingsViewController.sampleRateField.text floatValue];  
  11.   //2 採樣率  
  12.   [recordSettings setObject:  
  13.    [NSNumber numberWithFloat:sampleRate] forKey: AVSampleRateKey];  
  14.     
  15.   //3 通道的數目  
  16.   [recordSettings setObject:  
  17.    [NSNumber numberWithInt:  
  18.     (pcmSettingsViewController.stereoSwitch.on ? 2 : 1)]  
  19.    forKey:AVNumberOfChannelsKey];  
  20.   int bitDepth =  
  21.    [pcmSettingsViewController.sampleDepthField.text intValue];  
  22.     
  23.   //4 採樣位元  預設 16  
  24.   [recordSettings setObject:  
  25.    [NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey];  
  26.     
  27.   //5  
  28.   [recordSettings setObject:  
  29.    [NSNumber numberWithBool:  
  30.      pcmSettingsViewController.bigEndianSwitch.on]  
  31.     forKey:AVLinearPCMIsBigEndianKey];  
  32.  
  33.  
  34.   //6 採樣訊號是整數還是浮點數  
  35.   [recordSettings setObject:  
  36.    [NSNumber numberWithBool:  
  37.      pcmSettingsViewController.floatingSamplesSwitch.on]  
  38.     forKey:AVLinearPCMIsFloatKey]; 

AVAudioRecorder成功建立後,使用他非常直接.它的三個基本方法如下

 
  1. -(void) startRecording {  
  2.  [audioRecorder record];  
  3. }  
  4.  
  5. -(void) pauseRecording {  
  6.  [audioRecorder pause];  
  7.  recordPauseButton.selected = NO;  
  8. }  
  9.  
  10. -(void) stopRecording {  
  11.  [audioRecorder stop];  

小結:詳解IPhoneAVAudioRecorder的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.