温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IPhone之AVAudioRecorder

发布时间:2020-04-05 19:31:03 来源:网络 阅读:373 作者:tony关东升 栏目:开发技术

#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)


view plain

 

  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成功创建后,使用他非常直接.它的三个基本方法如下


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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI