iOS 录音功能

https://www.jianshu.com/p/c1bdab0ddf59

录音文件caf转mp3

一、创建recoder

- (AVAudioRecorder *)audioRecorder {
    if (_audioRecorder == nil) {
     一定要加这两个方法,没有这两个方法,不能录入语音
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        
        NSString *recordFilePath = [SyFileManager recordFilePathWithFileName:kSourceRecordFileName cleanFlag:YES];
        
        NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
        [recordSetting setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
        [recordSetting setObject:@(8000.0) forKey:AVSampleRateKey];
        [recordSetting setObject:@(2) forKey:AVNumberOfChannelsKey];
        [recordSetting setObject:@(AVAudioQualityMedium) forKey:AVEncoderAudioQualityKey];
        
        NSURL *url = [NSURL fileURLWithPath:recordFilePath];
        _audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:nil];
        if(!_audioRecorder) {
          NSLog(@"_audioRecorder nil");
        }
        
        [_audioRecorder setDelegate:self];
        [_audioRecorder prepareToRecord];
        _audioRecorder.meteringEnabled = YES;
        
    }
    
    return _audioRecorder;
}

二、开启录音

[self.audioRecorder record];

三、暂停录音

[self.audioRecorder pause];

四、结束录音

 [self.audioRecorder stop];

你可能感兴趣的:(iOS 录音功能)