IPhone之AVAudioRecorder

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

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

 


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


1:一般的音頻設置


2:線性PCM設置


3:編碼器設置


4:採樣率轉換設置


 


NSMutableDictionary  需要加入五個設置值(線性PCM)


 

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

 


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


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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章