Android编译Lame的全平台so库方案2,并实现转码mp3

Android编译Lame的全平台so库方案2,并实现转码mp3_第1张图片

作者:谭东

时间: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上的编译如何新建项目等去我的前几篇文章看即可。


大致结构:

Android编译Lame的全平台so库方案2,并实现转码mp3_第2张图片


编写配置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的音频格式的方法了。


版权所有,尊重版权。
微信公众号:





你可能感兴趣的:(NDK)