IPhone之AVAudioRecorder

转载来源于:http://www.cnblogs.com/lm3515/archive/2011/03/16/1985666.html
#import <AVFoundation/AVFoundation.h>  需要引入
   
//获取document目录的路径
- (NSString*) documentsPath {
  if (! _documentsPath) {
   NSArray *searchPaths =
    NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
   _documentsPath = [searchPaths objectAtIndex: 0];
   [_documentsPath retain];
  }
  return _documentsPath;
}
   
//(document目录的路径)
  NSString *destinationString = [[self documentsPath]
    stringByAppendingPathComponent:filenameField.text];
  NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
//初始化AVAudioRecorder
  NSError *recorderSetupError = nil;
  AVAudioRecorder audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL
    settings:recordSettings error:&recorderSetupError]; 
  [recordSettings release];
  
第二个参数  settings是一个容纳键值对的NSDictionary有四种一般的键
1:一般的音频设置
2:线性PCM设置
3:编码器设置
4:采样率转换设置
  
NSMutableDictionary  需要加入五个设置值(线性PCM)
NSMutableDictionary *recordSettings =
   [[NSMutableDictionary alloc] initWithCapacity:10];
   //1 ID号
   [recordSettings setObject:
    [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
   float sampleRate =
    [pcmSettingsViewController.sampleRateField.text floatValue];
   //2 采样率
   [recordSettings setObject:
    [NSNumber numberWithFloat:sampleRate] forKey: AVSampleRateKey];
    
   //3 通道的数目
   [recordSettings setObject:
    [NSNumber numberWithInt:
     (pcmSettingsViewController.stereoSwitch.on ? 2 : 1)]
    forKey:AVNumberOfChannelsKey];
   int bitDepth =
    [pcmSettingsViewController.sampleDepthField.text intValue];
    
   //4 采样位数  默认 16
   [recordSettings setObject:
    [NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey];
    
   //5
   [recordSettings setObject:
    [NSNumber numberWithBool:
      pcmSettingsViewController.bigEndianSwitch.on]
     forKey:AVLinearPCMIsBigEndianKey];
  
   //6 采样信号是整数还是浮点数
   [recordSettings setObject:
    [NSNumber numberWithBool:
      pcmSettingsViewController.floatingSamplesSwitch.on]
     forKey:AVLinearPCMIsFloatKey];
  
AVAudioRecorder成功创建后,使用他非常直接.它的三个基本方法如下
-( void ) startRecording {
  [audioRecorder record];
}
-( void ) pauseRecording {
  [audioRecorder pause];
  recordPauseButton.selected = NO;
}
-( void ) stopRecording {
  [audioRecorder stop]; 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值