溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

IPhone之AVAudioRecorder

發(fā)布時(shí)間:2020-04-05 19:31:03 來(lái)源:網(wǎng)絡(luò) 閱讀:394 作者:tony關(guān)東升 欄目:開發(fā)技術(shù)

#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];   

 


第二個(gè)參數(shù)  settings是一個(gè)容納鍵值對(duì)的NSDictionary有四種一般的鍵


1:一般的音頻設(shè)置


2:線性PCM設(shè)置


3:編碼器設(shè)置


4:采樣率轉(zhuǎn)換設(shè)置


 


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


view plain

 

  1.     NSMutableDictionary *recordSettings =   
  2.       [[NSMutableDictionary alloc] initWithCapacity:10];   
  3.       //1 ID號(hào)   
  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 通道的數(shù)目   
  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 采樣位數(shù)  默認(rèn) 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 采樣信號(hào)是整數(shù)還是浮點(diǎn)數(shù)   
  31.       [recordSettings setObject:   
  32.        [NSNumber numberWithBool:   
  33.          pcmSettingsViewController.floatingSamplesSwitch.on]   
  34.         forKey:AVLinearPCMIsFloatKey]   
  35.  
  36.  

 


AVAudioRecorder成功創(chuàng)建后,使用他非常直接.它的三個(gè)基本方法如下


view plain
  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. }   

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI