1,下载lame源码
下载地址:https://lame.sourceforge.io/download.php
2,编译
2.1,copy源码的下面部分
copy源码到src/main/cpp/lame文件夹下,删除非cpp/c/.h文件
2.2,编写Android.mk或者CMakeLists.txt编译文件
Android.mk文件如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmp3lame
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
./libmp3lame/bitstream.c \
./libmp3lame/encoder.c \
./libmp3lame/fft.c \
./libmp3lame/gain_analysis.c \
./libmp3lame/id3tag.c \
./libmp3lame/lame.c \
./libmp3lame/mpglib_interface.c \
./libmp3lame/newmdct.c \
./libmp3lame/presets.c \
./libmp3lame/psymodel.c \
./libmp3lame/quantize.c \
./libmp3lame/quantize_pvt.c \
./libmp3lame/reservoir.c \
./libmp3lame/set_get.c \
./libmp3lame/tables.c \
./libmp3lame/takehiro.c \
./libmp3lame/util.c \
./libmp3lame/vbrquantize.c \
./libmp3lame/VbrTag.c \
./libmp3lame/version.c
LOCAL_CPPFLAGS += -std=c++11 #-std=gnu++11
#LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Application.mk文件如下:
APP_ABI := armeabi
#, armeabi-v7a, arm64-v8a
APP_PLATFORM=android-14
APP_STL := gnustl_static #stlport_static
#注意要有这个宏
APP_CFLAGS += -DSTDC_HEADERS
app build.gradle里添加:
// externalNativeBuild {
// cmake {
// path "src/main/cpp/CMakeLists.txt"
// version "3.10.2"
// }
// }
externalNativeBuild {
ndkBuild {
path file('src/main/cpp/lame/Android.mk')
}
}
记得配置ndk路径,在local.properties文件里添加你自己的ndk路径,如:
sdk.dir=D\:\\VS
ndk.dir=D\:\\Program_Files\\NDK\\android-ndk-r13b-windows-x86_64\\android-ndk-r13b
此时编译报错:
…\lame\libmp3lame\util.h:574:12: error: unknown type name ‘ieee754_float32_t’
修改ieee754_float32_t为float后编译Ok
3,使用
我是在安卓上,使用audiorecord录制音频数据,将数据通过jni传给Mp3Encoder使用,下面贴出类Mp3Encoder的实现
Mp3Encoder::Mp3Encoder():m_handle(nullptr),data_encoded(NULL),m_file(NULL),AudioEncoder() {
}
Mp3Encoder::~Mp3Encoder() {
if(data_encoded){
delete [] data_encoded;
data_encoded = NULL;
}
}
void Mp3Encoder::init(int nsamples , int channel , const char *path) {
std::lock_guard<std::mutex> lockGuard(m_mutex);
m_file = fopen(path , "wb");
if(!m_file){
__android_log_print(ANDROID_LOG_INFO , "lxs0629 " , "errorcode = %d",errno);
}
m_nsamples = nsamples;
m_channel = channel;
m_handle = lame_init();
lame_set_num_channels(m_handle , channel);
lame_set_in_samplerate(m_handle, nsamples);
lame_init_params(m_handle);
}
/**
* return code 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
* -5: m_handle = nullptr
*/
int Mp3Encoder::encode(short * buffer_l , short * buffer_r , int length) {
if(!data_encoded){
data_encoded = new unsigned char [1024*1024];
}
std::lock_guard<std::mutex> lockGuard(m_mutex);
if(m_handle){
__android_log_print(ANDROID_LOG_INFO , "lxs0629 " , "length = %d",length);
int size_encoded = lame_encode_buffer(m_handle ,buffer_l , buffer_r , length / sizeof(short), data_encoded ,1024*1024 );
if(size_encoded > 0 && m_file){
fwrite(data_encoded , 1 , size_encoded , m_file);
}
return size_encoded;
}
return -5;
}
void Mp3Encoder::close() {
std::lock_guard<std::mutex> lockGuard(m_mutex);
if(m_handle){
lame_close(m_handle);
m_handle = NULL;
}
if(m_file){
fclose(m_file);
m_file = NULL;
}
}
最终测试mp3文件播放声音正常