iOS实现音频录制常用的三种方式
录制之前应对AVAudioSeeion进行设置
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; //设置为录音模式, 如果需要可以对采样率sampleRate,缓冲时长ioBufferDuration等进行设置
[audioSession setActive:YES error:nil];
1、AVAudioRecorder:
AVAudioRecorder的使用相对比较简单NSError *error;
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:8000], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,nil];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:filePath] settings:settings error:&error];
if (error) {
查看具体原因
}
recorder.delegate = self;
if ([recorder prepareToRecord]) {
[recorder record];
}
这样就实现简单的音频录制。当然AVAudioRecorder还提供了暂停,继续等功能,代理中有录制完成、出错等回掉信息。
2、AVCaptureSession:
_captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *devices = [AVCaptureDevice defaultDeviceW