欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现录音转码MP3及转码BASE64上传示例

程序员文章站 2024-02-15 20:13:34
ios 录音转码mp3及转码base64上传 一,开始录音 nslog(@"开始录音"); [self startrecord]; - (void)...

ios 录音转码mp3及转码base64上传

一,开始录音

nslog(@"开始录音");

[self startrecord];

- (void)startrecord
{
  //删除上次生成的文件,保留最新文件
  nsfilemanager *filemanager = [nsfilemanager defaultmanager];
  if ([nstemporarydirectory() stringbyappendingstring:@"myselfrecord.mp3"]) {
    [filemanager removeitematpath:[nstemporarydirectory() stringbyappendingstring:@"myselfrecord.mp3"] error:nil];
  }
  if ([nstemporarydirectory() stringbyappendingstring:@"selfrecord.wav"]) {
    [filemanager removeitematpath:[nstemporarydirectory() stringbyappendingstring:@"selfrecord.wav"] error:nil];
  }
  
  //开始录音
  //录音设置
  nsmutabledictionary *recordsetting = [[nsmutabledictionary alloc] init];
  //设置录音格式 avformatidkey==kaudioformatlinearpcm
  [recordsetting setvalue:[nsnumber numberwithint:kaudioformatlinearpcm] forkey:avformatidkey];
  //设置录音采样率(hz) 如:avsampleratekey==8000/44100/96000(影响音频的质量), 采样率必须要设为11025才能使转化成mp3格式后不会失真
  [recordsetting setvalue:[nsnumber numberwithfloat:11025.0] forkey:avsampleratekey];
  //录音通道数 1 或 2 ,要转换成mp3格式必须为双通道
  [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];
  
  //存储录音文件
  recordurl = [nsurl urlwithstring:[nstemporarydirectory() stringbyappendingstring:@"selfrecord.wav"]];
  
  //初始化
  audiorecorder = [[avaudiorecorder alloc] initwithurl:recordurl settings:recordsetting error:nil];
  //开启音量检测
  audiorecorder.meteringenabled = yes;
  audiosession = [avaudiosession sharedinstance];//得到avaudiosession单例对象

  if (![audiorecorder isrecording]) {
    [audiosession setcategory:avaudiosessioncategoryplayandrecord error:nil];//设置类别,表示该应用同时支持播放和录音
    [audiosession setactive:yes error:nil];//启动音频会话管理,此时会阻断后台音乐的播放.
    
    [audiorecorder preparetorecord];
    [audiorecorder peakpowerforchannel:0.0];
    [audiorecorder record];
  }
}

二,停止录音

[self endrecord];


 - (void)endrecord
 {
   [audiorecorder stop];             //录音停止
   [audiosession setactive:no error:nil];     //一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
 }

三,转码成mp3

- (void)transformcaftomp3 {
  mp3filepath = [nsurl urlwithstring:[nstemporarydirectory() stringbyappendingstring:@"myselfrecord.mp3"]];
  
  @try {
    int read, write;
    
    file *pcm = fopen([[recordurl absolutestring] cstringusingencoding:1], "rb");  //source 被转换的音频文件位置
    fseek(pcm, 4*1024, seek_cur);                          //skip file header
    file *mp3 = fopen([[mp3filepath absolutestring] 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 = (int)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生成成功");
    base64str = [self mp3tobase64];
  }
}

四,上传需要转码base64

 - (nsstring *)mp3tobase64{
   nsdata *mp3data = [nsdata datawithcontentsoffile:[nstemporarydirectory() stringbyappendingstring:@"myselfrecord.mp3"]];
   nsstring *_encodedimagestr = [mp3data base64encoding];
   nslog(@"===encoded image:\n%@", _encodedimagestr);
   return _encodedimagestr;
 }

备注:其中可以直接生成的.caf  .wav    有压缩的mp3需要转格式,不能直接录音生成

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。