录音

//iOS8开始需要向用户请求录音授权
    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            //Apple不支持录音输出mp3编码,支持AAC编码,故文件名写m4a
            NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/hh.m4a"];
            NSDictionary *dict = [self audioRecordingSettings];
            NSError *error;
            _recorder = [[AVAudioRecorder alloc] initWithURL:url settings:dict error:&error];
            if (_recorder) {
                [_recorder prepareToRecord];
                [_recorder record];
                //20秒后停止录制
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [_recorder stop];
                });
            }
        }
        else {
            NSLog(@"用户,没有给你录音权限");
        }
    }];

- (NSDictionary *)audioRecordingSettings {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[AVFormatIDKey] = @(kAudioFormatAppleLossless);
    dict[AVSampleRateKey] = @(44100.0f);
    dict[AVNumberOfChannelsKey] = @1;
    dict[AVEncoderAudioQualityKey] = @(AVAudioQualityLow);
    return dict;
}

iOS录制mp3音频

开源库:Lame (http://lame.sourceforge.net/)
divine提供的Lame框架: https://github.com/wuqiong/mp3lame-for-iOS.git

录音参数

- (NSDictionary *)audioRecordingSettings {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[AVSampleRateKey] = @(44100.0f);
    dict[AVNumberOfChannelsKey] = @2;
    dict[AVEncoderAudioQualityKey] = @(AVAudioQualityLow);
    
    dict[AVFormatIDKey] = @(kAudioFormatLinearPCM);
    return dict;
}
    //iOS8开始需要向用户请求录音授权
    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            //Apple不支持录音输出mp3编码,支持AAC和PCM编码,故文件名写m4a,原始音频数据
            NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/hh.pcm"];//*
            NSDictionary *dict = [self audioRecordingSettings];
            NSError *error;
            _recorder = [[AVAudioRecorder alloc] initWithURL:url settings:dict error:&error];
            if (_recorder) {
                [_recorder prepareToRecord];
                [_recorder record];
                //20秒后停止录制
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [_recorder stop];
                });
            }
        }
        else {
            NSLog(@"用户,没有给你录音权限");
        }
    }];

在录音结束的代理方法中进行处理

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
    if (flag) {
        [self convertPcmFile:@"/Users/apple/Desktop/hh.pcm" toMp3File:@"/Users/apple/Desktop/hh.mp3"];
    }
}
MP3编码方法封装
  1. 先导入lame.framework框架
  2. 再导入头文件lame/lame.h
  3. 再封装转码方法
- (void)convertPcmFile:(NSString*)pcmFile toMp3File:(NSString*)mp3File {
    int read, write;
    //C方法打开文件
    FILE *pcm = fopen(pcmFile.UTF8String, "rb");
    FILE *mp3 = fopen(mp3File.UTF8String, "wb");
    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;
    //准备内存缓存区
    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];
    //初始化编码器
    lame_t lame = lame_init();
    //设置采样率,务必和pcm文件采样率一致
    lame_set_in_samplerate(lame, 44100);
    //设置默认动态比特率
    lame_set_VBR(lame, vbr_default);
    //初始化这些参数
    lame_init_params(lame);
    do {
        //先读PCM文件数据
        read = (int)fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
        if (read == 0){
            write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
        }
        else{
            //把PCM原始音频数据帧按照2个声道转换为MP3压缩的数据,务必和录音设置一致
            write = lame_encode_buffer_interleaved(lame, pcm_buffer, read,
            mp3_buffer, MP3_SIZE); }
        //写入到输出文件中
        fwrite(mp3_buffer, write, 1, mp3);
    } while (read != 0);
    lame_close(lame);
    fclose(mp3);
    fclose(pcm);
    return;
}

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