前言
本系列文章属于个人学习AVFoundation笔记,本节属于AVAudioRecorder的使用,使用AVAudioRecorder录音。
创建AudioRecorder
// 3. 根据路径以及设置项, 创建录音对象
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:self.recordPath] settings:self.recordSettingDic error:nil];
// 4. 准备录音
[self.audioRecorder prepareToRecord];
配置AudioRecorder
//2. 录音设置
self.recordSettingDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInteger:kAudioFormatMPEG4AAC],AVFormatIDKey,[NSNumber numberWithInteger:1000],AVSampleRateKey,
[NSNumber numberWithInteger:2],AVNumberOfChannelsKey,
[NSNumber numberWithInteger:8],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,nil];
配置录音保存地址
//1. 沙盒路径
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
self.recordPath = [NSString stringWithFormat:@"%@/play.aac",docDir];
NSLog(@"%@",self.recordPath);
录音&停止录音
#pragma mark -- 录音
- (void)recordAction {
if ([self canRecord]) {
[self.audioRecorder record];
}
}
#pragma mark -- 停止录音
- (void)stopAction {
[self.audioRecorder stop];
}
源码地址
https://github.com/GeeksChen/AudioRecorder