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 = [AVCap

本文介绍了iOS平台上三种常用的音频录制方法:AVAudioRecorder提供简单的录音功能及操作回调;AVCaptureSession用于自动将音频写入文件;AudioUnit则涉及更底层的初始化和配置,适合进阶使用。
最低0.47元/天 解锁文章
2712





