你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS實現錄音轉碼MP3及轉碼BASE64上傳示例

iOS實現錄音轉碼MP3及轉碼BASE64上傳示例

編輯:IOS開發綜合

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需要轉格式,不能直接錄音生成

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

【iOS實現錄音轉碼MP3及轉碼BASE64上傳示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved