Url:http://blog.csdn.net/ysy441088327/article/details/7392842#replyui
爲了達到 iPhone 與 Android 實現音頻互通. 那麼Mp3格式的音頻文件再好不過了.編碼
至於可以轉換成Amr 是最好,10秒 的 一個Amr文件 只有5K左右的大小. 很是適合移動設備的數據傳輸url
這裏主要用到lame,一款很是棒的Mp3音頻編碼器. spa
那麼在轉換以前呢? 就須要先錄製好音頻文件,使用 AVAudioRecorder 進行音頻錄製以前,進行以下參數設置:.net
- (void)initialRecord { //錄音權限設置,IOS7必須設置,獲得AVAudioSession單例對象 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; //設置類別,此處只支持支持錄音 [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; //啓動音頻會話管理,此時會阻斷後臺音樂的播放 [audioSession setActive:YES error:nil]; //錄音參數設置設置 NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init]; //設置錄音格式 AVFormatIDKey==kAudioFormatLinearPCM // [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; //caf的錄製格式 [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; //設置錄音採樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質量)//acc的採樣頻率 // [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey]; //錄音通道數 1 或 2 在錄製caf文件時,須要使用雙通道,不然在轉換爲MP3格式時,聲音不對 [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; //線性採樣位數 八、1六、2四、32 [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; //錄音的質量 // [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey]; //錄音文件保存的URL CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault); NSString *cfuuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid)); NSString * filename = [NSString stringWithFormat:@"%@.caf",cfuuidString]; NSString *audioRecordFilePath = AUDIORECORDFILEPATH(filename); lastaudioRecordFilePath = audioRecordFilePath; //判斷目錄是否存在不存在則建立 NSString *audioRecordDirectories = [audioRecordFilePath stringByDeletingLastPathComponent]; NSFileManager *fileManager=[NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:audioRecordDirectories]) { [fileManager createDirectoryAtPath:audioRecordDirectories withIntermediateDirectories:YES attributes:nil error:nil]; } NSURL *url = [NSURL fileURLWithPath:audioRecordFilePath]; NSError *error=nil; //初始化AVAudioRecorder _recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error]; if (error != nil) { //NSLog(@"初始化錄音Error: %@",error); }else{ if ([_recorder prepareToRecord]) { //錄音最長時間 [_recorder recordForDuration:self.recorderDuration-1]; _recorder.delegate=self; [_recorder record]; //開啓音量檢測 _recorder.meteringEnabled = YES; //開啓定時器,音量監測 _timer=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(volumeMeters:) userInfo:nil repeats:YES]; } } }
經過上面的參數所錄製的音頻文件體積很是大,不過不要擔憂,這只是第一步,只要成功轉換成Mp3之後,能夠保證文件體積每秒在4K左右.^^code
另一點,除非你時間多,那麼不必去嘗試設置其餘種類的參數再用來轉換,由於做者我就試了很多,反正只有上面的參數才能保證,音質的完整和流暢.orm
下面介紹 lame 靜態庫 使用流程 主要有兩個核心文件,使用很簡單:對象
須要加入 lame.hblog
#include "lame.h" ip
//轉編碼爲 mp3 - (void)audio_PCMtoMP3:(NSString *)cafFilePath andMP3FilePath:(NSString *)mp3FilePath { NSFileManager* fileManager=[NSFileManager defaultManager]; if([fileManager removeItemAtPath:mp3FilePath error:nil]) { NSLog(@"刪除"); } @try { int read, write; FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被轉換的音頻文件位置 if(pcm == NULL) { NSLog(@"file not found"); } else { 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生成成功"); } }
lamp下載區 密碼: 3gy2