1. 程式人生 > >iOS 通過(lame)將錄製音訊轉換成Mp3

iOS 通過(lame)將錄製音訊轉換成Mp3

轉載請註明出處,保留原帖地址及作者署名.

Author:餘書懿

為了達到 iPhone 與 Android 實現音訊互通. 那麼Mp3格式的音訊檔案再好不過了.

至於能夠轉換成Amr 是最好,10秒 的 一個Amr檔案 只有5K左右的大小. 非常適合移動裝置的資料傳輸

這裡主要用到lame,一款非常棒的Mp3音訊編碼器.   

那麼在轉換之前呢? 就需要先錄製好音訊檔案,使用 AVAudioRecorder 進行音訊錄製之前,進行如下引數設定:

NSString *recordTemporaryPathString = [NSString stringWithFormat:@"%@/temporary",self.audioTemporarySavePath];
//LinearPCM 是iOS的一種無損編碼格式,但是體積較為龐大
//錄音設定
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
//錄音格式 無法使用
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
//取樣率
[recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];//44100.0
//通道數
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
//線性取樣位數
//[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
//音訊質量,取樣質量
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];

通過上面的引數所錄製的音訊檔案體積非常大,不過不要擔心,這只是第一步,只要成功轉換成Mp3以後,可以保證檔案體積每秒在4K左右.^^

另外一點,除非你時間多,那麼沒必要去嘗試設定其他種類的引數再用來轉換,因為作者我就試了不少,反正只有上面的引數才能保證,音質的完整和流暢.

下面介紹 lame 靜態庫 使用流程 主要有兩個核心檔案,使用很簡單:

1:lame庫加入工程中.  注:靜態庫中,可直接加入其他的靜態庫. 

2:引入標頭檔案 lame.h

#include "lame.h"

3:核心轉換程式碼

- (void)audio_PCMtoMP3
{

    NSString *mp3FileName = [self.audioFileSavePath lastPathComponent];
    mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
    NSString *mp3FilePath = [self.audioTemporarySavePath stringByAppendingPathComponent:mp3FileName];
    
    @try {
        int read, write;
        
        FILE *pcm = fopen([self.audioFileSavePath 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 {
        self.audioFileSavePath = mp3FilePath;
        NSLog(@"MP3生成成功: %@",self.audioFileSavePath);
    }

}

注:如果沒有意外,轉換成功後將直接進到  @finally 程式碼塊中.

真機模擬器中均可完美實現轉換,值得注意的是:模擬器在錄製音訊的時候,偶爾會產生雜音,那麼轉換後的mp3 也會附帶雜音,不知道為什麼,目前在真機還沒有遇到過.

轉換消耗的時長是根據你錄製音訊檔案的大小來決定的,因此建議啟用一個新的執行緒來處理這個任務,程式碼如下:

[NSThread detachNewThreadSelector:@selector(audio_PCMtoMP3) toTarget:self withObject:nil];

檔案字尾也可以決定所生成的錄製音訊的格式  例如:.wav  .caf

至少大致結束,上傳一下lame靜態庫,有需要的朋友下載吧. 猛擊此處 密碼:Is9I9W

這篇博文,是我重新修改的,刪去了一些錯誤的筆記,以及對程式碼進行了重新測試和嘗試.基本上是沒問題.