Android上使用MP3格式录制声音
0. 下载LAME 并解压缩
http://lame.sourceforge.net/download.php
http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz
1. 创建jni/libmp3lame/src目录, 将解压缩后的lame-3.99.5/libmp3lame/ 目录下的
bitstream.c fft.h l3side.h presets.c quantize_pvt.c set_get.h util.h version.c bitstream.h gain_analysis.c lame-analysis.h machine.h psymodel.c quantize_pvt.h tables.c vbrquantize.c version.h encoder.c gain_analysis.h lame.c mpglib_interface.c psymodel.h reservoir.c tables.h vbrquantize.h encoder.h id3tag.c lame_global_flags.h newmdct.c quantize.c reservoir.h takehiro.c VbrTag.c fft.c id3tag.h newmdct.h quantize.h set_get.c util.c VbrTag.h
基本上就是所有的.c和.h文件, 除了lameerror.h可以不用
等文件 全部复制到 jni/libmp3lame/src
将 lame-3.99.5/include/ 目录下的 lame.h 也复制到 jni/libmp3lame/src
2.创建jni/Android.mk
ROOT_PATH := $(call my-dir) # make speex and lame LOCAL_PATH := $(call my-dir) include $(ROOT_PATH)/libmp3lame/Android.mk
jni/libmp3lame/Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LAME_LIBMP3_DIR := src 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 li_zhiwei_mp3recorder_SimpleLame.c include $(BUILD_SHARED_LIBRARY)
jni/application.mk
APP_ABI := armeabi armeabi-v7a APP_PLATFORM := android-8 APP_PLATFORM := android-9 APP_STL := stlport_static STLPORT_FORCE_REBUILD := true
3. 修改源代码
fft.c中去掉
//#include “vector/lame_intrin.h”
这个是用Intel CPU的特性进行优化, ARM上用不到
set_get.h中将
#include
改成
#include “lame.h”
utils.h中将
extern ieee754_float32_t fast_log2(ieee754_float32_t x);
用
extern float fast_log2(float x);
替换
4. 创建封装文件
li_zhiwei_mp3recorder_SimpleLame.c
#include "src/lame.h" #include "li_zhiwei_mp3recorder_SimpleLame.h" static lame_global_flags *glf = NULL; JNIEXPORT void JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } glf = lame_init(); lame_set_in_samplerate(glf, inSamplerate); lame_set_num_channels(glf, outChannel); lame_set_out_samplerate(glf, outSamplerate); lame_set_brate(glf, outBitrate); lame_set_quality(glf, quality); lame_init_params(glf); } JNIEXPORT jint JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_encode( JNIEnv *env, jclass cls, jshortArray buffer_l, jshortArray buffer_r, jint samples, jbyteArray mp3buf) { jshort* j_buffer_l = (*env)->GetShortArrayElements(env, buffer_l, NULL); jshort* j_buffer_r = (*env)->GetShortArrayElements(env, buffer_r, NULL); const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf); jbyte* j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL); int result = lame_encode_buffer(glf, j_buffer_l, j_buffer_r, samples, j_mp3buf, mp3buf_size); (*env)->ReleaseShortArrayElements(env, buffer_l, j_buffer_l, 0); (*env)->ReleaseShortArrayElements(env, buffer_r, j_buffer_r, 0); (*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0); return result; } JNIEXPORT jint JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_flush( JNIEnv *env, jclass cls, jbyteArray mp3buf) { const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf); jbyte* j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL); int result = lame_encode_flush(glf, j_mp3buf, mp3buf_size); (*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0); return result; } JNIEXPORT void JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_close( JNIEnv *env, jclass cls) { lame_close(glf); glf = NULL; }
li_zhiwei_mp3recorder_SimpleLame.h (这个文件其实是在 javah li.zhiwei.mp3recorder.SimpleLame 命令生成的)
/* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class li_zhiwei_mp3recorder_SimpleLame */ #ifndef _Included_li_zhiwei_mp3recorder_SimpleLame #define _Included_li_zhiwei_mp3recorder_SimpleLame #ifdef __cplusplus extern "C" { #endif /* * Class: li_zhiwei_mp3recorder_SimpleLame * Method: close * Signature: ()V */ JNIEXPORT void JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_close (JNIEnv *, jclass); /* * Class: li_zhiwei_mp3recorder_SimpleLame * Method: encode * Signature: ([S[SI[B)I */ JNIEXPORT jint JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_encode (JNIEnv *, jclass, jshortArray, jshortArray, jint, jbyteArray); /* * Class: li_zhiwei_mp3recorder_SimpleLame * Method: flush * Signature: ([B)I */ JNIEXPORT jint JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_flush (JNIEnv *, jclass, jbyteArray); /* * Class: li_zhiwei_mp3recorder_SimpleLame * Method: init * Signature: (IIIII)V */ JNIEXPORT void JNICALL Java_li_zhiwei_mp3recorder_SimpleLame_init (JNIEnv *, jclass, jint, jint, jint, jint, jint); #ifdef __cplusplus } #endif #endif
6. 运行 ndk_build 即可
参考资料
http://developer.samsung.com/android/technical-docs/Porting-and-using-LAME-MP3-on-Android-with-JNI
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。