iOS 音频录制

本文介绍了iOS平台上三种常用的音频录制方法:AVAudioRecorder提供简单的录音功能及操作回调;AVCaptureSession用于自动将音频写入文件;AudioUnit则涉及更底层的初始化和配置,适合进阶使用。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值