Android 使用mp3lame 庫 錄製mp3
一、功能說明
1、通過libmp3lame庫將PCM轉成MP3的音頻格式。
2、添加一個自定義的錄音控件。
1.1對外開放方法說明:
/**
* 設置保存文件的路徑
*
* @param filePath 錄製文件的地址
*/
void setAudioPath(String filePath);
/**
* 設置回調
*
* @param audioListener 回調接口
*/
void setAudioListener(AudioRecordListener audioListener);
/**
* 開始錄製音頻
*/
void startRecord();
/**
* 暫停錄製
*/
void onPause();
/**
* 繼續錄製
*/
void onResume();
/**
* 停止錄製
*/
void stopRecord();
1.2 mp3錄製方法說明:
/**
* Initialize LAME.
*
* @param inSamplerate
* input sample rate in Hz.
* @param inChannel
* number of channels in input stream.
* @param outSamplerate
* output sample rate in Hz.
* @param outBitrate
* brate compression ratio in KHz.
* @param quality
* <p>quality=0..9. 0=best (very slow). 9=worst.</p>
* <p>recommended:</p>
* <p>2 near-best quality, not too slow</p>
* <p>5 good quality, fast</p>
* 7 ok quality, really fast
*/
public native static void init(int inSamplerate, int inChannel,
int outSamplerate, int outBitrate, int quality);
/**
* Encode buffer to mp3.
*
* @param bufferLeft
* PCM data for left channel.
* @param bufferRight
* PCM data for right channel.
* @param samples
* number of samples per channel.
* @param mp3buf
* result encoded MP3 stream. You must specified
* "7200 + (1.25 * buffer_l.length)" length array.
* @return <p>number of bytes output in mp3buf. Can be 0.</p>
* <p>-1: mp3buf was too small</p>
* <p>-2: malloc() problem</p>
* <p>-3: lame_init_params() not called</p>
* -4: psycho acoustic problems
*/
public native static int encode(short[] bufferLeft, short[] bufferRight,
int samples, byte[] mp3buf);
/**
* Flush LAME buffer.
*
* REQUIRED:
* lame_encode_flush will flush the intenal PCM buffers, padding with
* 0's to make sure the final frame is complete, and then flush
* the internal MP3 buffers, and thus may return a
* final few mp3 frames. 'mp3buf' should be at least 7200 bytes long
* to hold all possible emitted data.
*
* will also write id3v1 tags (if any) into the bitstream
*
* return code = number of bytes output to mp3buf. Can be 0
* @param mp3buf
* result encoded MP3 stream. You must specified at least 7200
* bytes.
* @return number of bytes output to mp3buf. Can be 0.
*/
public native static int flush(byte[] mp3buf);
/**
* Close LAME.
*/
public native static void close();
1.3 自定義錄音控件
喜歡的可以用用,不喜歡也沒辦法,反正我是打包在裏面了。
二、所做修改
原作者博客中,原理和過程已經寫的很清楚了。這裏就不再進行贅述了。只是作者之前使用.mk文件進行編譯的。但是最新的as軟件需要使用cmake進行編譯。所以我在這裏進行整理了一下。在最新的編譯軟件下面也可以使用。
三、修改記錄
- 使用cmake編譯mp3lame庫。
- include文件中,只保留頭文件。
- 刪除DataEncodeThread類,轉換mp3放在錄製線程中。
- 添加錄音暫停,繼續功能。
- 重新封裝mp3record。
四、新增功能:
4.1 暫停,繼續功能
在開發的過程中,我想有些同學肯定用得到錄製暫停的功能,所以我在原來的基礎上面增加了暫停的功能。實現的原理是,當用戶點擊暫停時,就不再往MP3文件中寫流,也不在MP3文件寫入MP3結尾信息。當用戶點擊繼續是,在原來文件流的基礎上繼續增加數據,只有當用戶停止錄製的時候才寫入MP3尾部信息。
4.2 自定義了麥克風錄製控件
效果圖如下:
1、長按進行MP3文件錄製。
2、鬆開按鈕結束錄製。
4.3 自定義了頻譜控件
效果如上圖
五、如何使用
You need to make sure you have the JCenter and Google repositories included in the build.gradle file in the root of your project:
repositories {
jcenter()
mavenCentral();
}
Next add a dependency in the build.gradle file of your app module. The following will add a dependency to the full library:
implementation 'com.ibbhub.audio:mp3recorderlib:1.0.2'
github: https://github.com/chezi008/AndroidMp3Recorder
[參考]
- 編譯libmp3lame庫: http://www.cnblogs.com/ct2011/p/4080193.html
- 自定義圓形錄製控件的動畫參考:不記得了
- 頻譜波動控件參考:https://www.jianshu.com/p/76aceacbc243