IPhone之AVAudioRecorder

简介: #import <AVFoundation/AVFoundation.h>  需要引入<br>   <br> //获取document目录的路径<br><pre name="code" class="cpp">- (NSString*) documentsPath { if (! _documentsPath) { NSArray *searchPaths =
#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];
}


目录
相关文章
|
iOS开发 C++ 内存技术
IPhone之AVAudioRecorder 录音
<p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:
1464 0
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
147 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
225 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉