iOS AVAudioRecorder

- (void)recorderCreate
{
    if (nil == self.m_audioRecorder)
    {
        if (self.m_recordFile.length > 0)
        {
            NSMutableDictionary *settings = [NSMutableDictionary dictionary];
            settings[AVSampleRateKey] = @44100.0;
            settings[AVFormatIDKey] = @(kAudioFormatLinearPCM);
            settings[AVLinearPCMBitDepthKey] = @16;
            settings[AVNumberOfChannelsKey]  = @1;
            settings[AVEncoderAudioQualityKey] = @(AVAudioQualityHigh);

            NSURL *url = [NSURL URLWithString:self.m_recordFile];
            //如果settings设置的参数不正确,将无法开始录音
            AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:nil];
            recorder.meteringEnabled = YES;
            recorder.delegate = self;

            self.m_audioRecorder = recorder;
        }
    }
}


- (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] >= NSOrderedAscending)
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)])
        {
            [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                if (granted)
                {
                    bCanRecord = YES;
                }
                else
                {
                    bCanRecord = NO;
                }
            }];
        }
    }

    return bCanRecord;
}

-(void)initAudioSession
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [session setActive:YES error:nil];
}

- (void)recorderStart
{
    [self.m_audioRecorder prepareToRecord];
    [self.m_audioRecorder record];
}

- (void)recorderPause
{
    if (self.m_audioRecorder.isRecording)
    {
        [self.m_audioRecorder pause];
    }
    else
    {
        [self.m_audioRecorder record];
    }
}

- (void)recorderStop
{
    [self.m_audioRecorder stop];
    self.m_audioRecorder = nil;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值