lame编译 android,Android编译Lame的全平台so库方案2,并实现转码mp3
作者:譚東
時間:2017年9月24日
環境:Windows 7
Lame版本:Lame 3.99.5
LAME是目前最好的MP3編碼引擎。LAME編碼出來的MP3音色純厚、空間寬廣、低音清晰、細節表現良好,它獨創的心理音響模型技術保證了CD音頻還原的真實性,配合VBR和ABR參數,音質幾乎可以媲美CD音頻,但文件體積卻非常小。對于一個免費引擎,LAME的優勢不言而喻。
如果你需要將音頻轉碼為mp3,就需要借助lame庫。因為FFmpeg并不支持編碼mp3。
因為ffmpeg自身也不支持轉碼Mp3,也是借助lame庫實現的。
首先在官網http://lame.sourceforge.net/?下載最新版的版本 lame-3.99.5 下載完進行解壓,然后把libmp3lame目錄下的文件拷貝到jni下面,去除i386文件夾,和非.c .h的文件,拷貝 lame.h (include目錄下)到jni目錄下。
編輯 jni/utils.h,把extern ieee754_float32_t fast_log2(ieee754_float32_t x);替換為extern float fast_log2(float x)。
ok,基本源碼準備工作就做好了。
在Android上的編譯如何新建項目等去我的前幾篇文章看即可。
大致結構:
編寫配置CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( lame
SHARED
libs/include/liblame/bitstream.c
libs/include/liblame/encoder.c
libs/include/liblame/fft.c
libs/include/liblame/gain_analysis.c
libs/include/liblame/id3tag.c
libs/include/liblame/lame.c
libs/include/liblame/mpglib_interface.c
libs/include/liblame/newmdct.c
libs/include/liblame/presets.c
libs/include/liblame/psymodel.c
libs/include/liblame/quantize.c
libs/include/liblame/quantize_pvt.c
libs/include/liblame/reservoir.c
libs/include/liblame/set_get.c
libs/include/liblame/tables.c
libs/include/liblame/takehiro.c
libs/include/liblame/util.c
libs/include/liblame/vbrquantize.c
libs/include/liblame/VbrTag.c
libs/include/liblame/version.c
libs/include/liblame/xmm_quantize_sub.c)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
lame
# Links the target library to the log library
# included in the NDK.
${log-lib} )
build.gradle里加入:
externalNativeBuild {
cmake {
cFlags "-DSTDC_HEADERS"
cppFlags ""
}
}是為了編譯64位so庫是防止出現
undefined symbol bcopy 這個錯誤的。
在新建的jni的native-lib.cpp里編寫測試獲取lame版本的方法:
#include
#include
#include "../../../libs/include/liblame/lame.h"
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_tandong_lame_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
JNIEXPORT jstring JNICALL
Java_com_tandong_lame_MainActivity_stringFromJNI2(
JNIEnv *env,
jobject /* this */) {
return env->NewStringUTF(get_lame_version());
}
}
Activity里調用即可:
package com.tandong.lame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI2());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String stringFromJNI2();
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
}
后面就可以繼續添加轉碼為mp3的音頻格式的方法了。
版權所有,尊重版權。
微信公眾號:
總結
以上是生活随笔為你收集整理的lame编译 android,Android编译Lame的全平台so库方案2,并实现转码mp3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python开源怎么盈利_弄清楚Pyth
- 下一篇: 常见的浏览器兼容问题和解决方法