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 *            

quality=0..9. 0=best (very slow). 9=worst.

*

recommended:

*

2 near-best quality, not too slow

*

5 good quality, fast

* 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

number of bytes output in mp3buf. Can be 0.

*

-1: mp3buf was too small

*

-2: malloc() problem

*

-3: lame_init_params() not called

* -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进行编译。所以我在这里进行整理了一下。在最新的编译软件下面也可以使用。

三、修改记录

  1. 使用cmake编译mp3lame库。
  2. include文件中,只保留头文件。
  3. 删除DataEncodeThread类,转换mp3放在录制线程中。
  4. 添加录音暂停,继续功能。
  5. 重新封装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

[参考]

  1. 编译libmp3lame库: http://www.cnblogs.com/ct2011/p/4080193.html
  2. 自定义圆形录制控件的动画参考:不记得了
  3. 频谱波动控件参考:https://www.jianshu.com/p/76aceacbc243

更多相关文章

  1. Android实战简易教程(自定义控件实现数字液晶时钟Demo)
  2. Android开发工程师个人简历
  3. android之MediaPlay播放视频
  4. Android(安卓)- Activity的生存期
  5. Android(安卓)控件之GridView
  6. Android[初级教程]第十一章 TabHost控件
  7. 《Android》Lesson11-UI布局
  8. Android(安卓)Manager之MediaRecorder(音视频录制)
  9. Android获取控件的大小

随机推荐

  1. android 利用java反射调用系统类的隐藏方
  2. Android使用getIdentifier()方法根据资源
  3. 让你的代码减少三倍!使用kotlin开发Androi
  4. Android(安卓)电量分析之Battery Histori
  5. Android(安卓)6.0运行时权限 (危险权限)
  6. android自定义大小对话框
  7. Android原生(Native)C开发之六:libpng移植
  8. Android+本地记事本(上)------实现登陆注
  9. Android(安卓)PinyinIME Debug 调试
  10. Android(安卓)MatrixCursor的详解