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;
}

你可能感兴趣的:(recorder)