话不多说,直入主题。
使用Android原生AudioRecorder录音之后的音频格式是PCM的,Mediaplayer是无法直接播放的,转成跨平台最好的MP3格式播的话就比较好了。
一种方法是先录音,录音完成之后再转;另一种方法是边录边转。前者在录完之后转换是需要时间的,体验不太好,所以选择后者。Android可以通过jni的方式调用Lame的方法来实现边录边转。
https://www.cnblogs.com/ct2011/p/4080193.html 这篇文章里有边录边转的实现过程可以参考,当然也可以使用其他方法,文章里把Lame编译成so的方式说的不太清楚,我这边记录一下自己生成so文件的过程。
- 在android项目中创建jni文件夹
- https://sourceforge.net/projects/lame/files/lame/ 下载Lame并解压;把include文件夹下的lame.h文件拷贝到libmp3lame目录;把libmp3lame文件夹重命名为lame_libmp3lame(这个重命名可自定义,但是要记好,后面会用到)拷贝到jni目录
- 对libmp3lame文件夹做处理:删除文件夹i386及其目录下的文件;删除所有非.c和.h的文件;utils.h文件中的ieee754_float32_t fast_log2改成float;set_get.h文件中#include <lame.h> 改成#include “lame.h”;编辑vector\xmm_quantize_sub.c文件,左边是修改前,右边是修改后,这里的修改一定要,不然路径不对,会报找不到文件的错误。
- 创建LameUtil.java工具类,里面定义我们需要调用lame的方法
public class LameUtil {
static{
System.loadLibrary("mp3lame");
}
/**
* 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();
}
- build项目,生成class文件。打开终端cd到jni目录,输入命令javah -jni -classpath C:\workspace\Application\app\build\intermediates\classes\debug com.demo.application.LameUtil;这样会在jni目录下生成com_demo_application_LameUtil.h头文件
- 在jni目录下新建.c文件,编写c++代码实现LameUtil.java中的方法,此.c文件名字可以自定义,规范起见,命名成com_demo_application_LameUtil.c
- 在jni文件夹下创建Android.mk 文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LAME_LIBMP3_DIR := lame_libmp3lame
LOCAL_MODULE := mp3lame
LOCAL_SRC_FILES := $(LAME_LIBMP3_DIR)/bitstream.c $(LAME_LIBMP3_DIR)/fft.c $(LAME_LIBMP3_DIR)/id3tag.c $(LAME_LIBMP3_DIR)/mpglib_interface.c $(LAME_LIBMP3_DIR)/presets.c $(LAME_LIBMP3_DIR)/quantize.c $(LAME_LIBMP3_DIR)/reservoir.c $(LAME_LIBMP3_DIR)/tables.c $(LAME_LIBMP3_DIR)/util.c $(LAME_LIBMP3_DIR)/VbrTag.c $(LAME_LIBMP3_DIR)/encoder.c $(LAME_LIBMP3_DIR)/gain_analysis.c $(LAME_LIBMP3_DIR)/lame.c $(LAME_LIBMP3_DIR)/newmdct.c $(LAME_LIBMP3_DIR)/psymodel.c $(LAME_LIBMP3_DIR)/quantize_pvt.c $(LAME_LIBMP3_DIR)/set_get.c $(LAME_LIBMP3_DIR)/takehiro.c $(LAME_LIBMP3_DIR)/vbrquantize.c $(LAME_LIBMP3_DIR)/version.c com_demo_application_LameUtil.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
文件中的含义我这里不做解释,网上资料很多;需要重点说明的一点是LOCAL_SRC_FILES这一行最后的com_demo_application_LameUtil.c这句一定要,这是通过java调用lame的入口,切记!!
8. 在jni文件夹下新建Application.mk文件
APP_ABI := all
APP_MODULES := mp3lame
APP_CFLAGS += -DSTDC_HEADERS
#APP_ABI:=x86_64
APP_PLATFORM := android-21
其中APP_ABI是表示要生成的so文件支持的手机系统架构,all是生成全部的。
9. 电脑配置好ndk环境,如何配置网上资料也一大堆。打开终端cd到jni目录,输入命令ndk-build,便会生成所需要的so文件了。
10.最后附一张最终项目结构图