美文网首页iOS
iOS 录音mp3

iOS 录音mp3

作者: TroyZhang | 来源:发表于2016-12-26 13:31 被阅读145次
  • 先用 AVAudioRecorder 录音生成 wav格式音频
  • lamewav格式转换成mp3
- (void)recordAudio
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

    NSError *error = nil;
    audioRecorder = [[ AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:[self getAudioFilePath]] settings:[self audioRecordingSettings] error:&error];
    audioRecorder.meteringEnabled = YES;

    if ([audioRecorder prepareToRecord] == YES)
    {
        audioRecorder.meteringEnabled = YES;
        [audioRecorder record];
    }
    else 
    {
        NSLog(@"录音失败");
    }
}

- (void)convertToMp3
{
    NSString *mp3FilePath = [self getMp3FilePath];
    
    @try {
        int read, write;
        
        FILE *pcm = fopen([[self getAudioFilePath] cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置
        
        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();
        lame_set_in_samplerate(lame, 11025.0);
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);
        
        do {
            read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
            if (read == 0)
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            else
                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);
    }
    @catch (NSException *exception)
    {
        NSLog(@"%@",[exception description]);
    }
    @finally
    {
        NSLog(@"MP3生成成功: %@",mp3FilePath);
        [self loadData];
    }
}

- (NSDictionary *)audioRecordingSettings{
    
    NSDictionary *result = nil;
    
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
    //设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
    [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];
    //录音通道数  1 或 2
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    //线性采样位数  8、16、24、32
//    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    //录音的质量
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    
    result = [NSDictionary dictionaryWithDictionary:recordSetting];
    return result;
}

- (NSString *)getAudioFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [paths[0] stringByAppendingPathComponent:@"asrview.wav"];
}

- (NSString *)getMp3FilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [paths[0] stringByAppendingPathComponent:@"asrview.mp3"];
}

http://blog.csdn.net/ysy441088327/article/details/7392842
https://github.com/Superbil/build-lame-for-iOS

相关文章

  • iOS-使用Lame转码:PCM->MP3

    本文是iOS 使用AUGraph录音同时播放(并转码成MP3)中关于使用Lame将PCM转码成MP3的详细说明。看...

  • iOS 录音mp3

    先用 AVAudioRecorder 录音生成 wav格式音频 用lame将wav格式转换成mp3 http://...

  • library not found for lmp3lame

    swift工程中,要上传mp3。ios录音出来的格式不支持mp3,需要转。这里用了lame的静态库。出现标题的问题...

  • iOS录音后转MP3并且实现音频拼接的示例程序

    iOS录音后转MP3并且实现音频拼接的示例程序 使用了iOS自带的AVFoundation和C语言的开源库lame...

  • iOS录音转码(MP3)

    由于采用手机端系统的录音功能,为了让iOS和Android的录音在其他平台上能够正常播放,这是就需要对录音的文件进...

  • iOS录音后转换mp3

    最近公司要求想在嵌入的网页里调用原声的的录音功能,并且吧录音上传到服务器,然后就开始各种趴资料啦,毕竟是小菜?嘛。...

  • 怎样将录音转换mp3格式?

    在工作中生活中大家都经常录音,比如说通话录音什么的,这些录音大部分都不是mp3格式的,不过也可以转换成mp3格式,...

  • iOS AVAudioRecorder录音,转MP3,播放

    功能说明 对录制声音的UI以及功能进行简单的封装,提供快速实现录音的帮助类. 支持监测录音时的声音大小. 支持特定...

  • ios录音wav格式转mp3

    1:语音转换,需要关注录音的参数配置,如果你的转化有问题,就直接把如下代码粘贴使用。录音代码: 转为mp3格式(使...

  • iOS 录音文件caf转mp3

    参考文章:https://www.jianshu.com/p/c1bdab0ddf59[https://www.j...

网友评论

    本文标题:iOS 录音mp3

    本文链接:https://www.haomeiwen.com/subject/hqejvttx.html