安卓音频开发(二)lame编译

什么是lame?lame和音频处理有什么关系?

lame官网:
https://lame.sourceforge.io
description:
LAME is a high quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL.
–可以简单理解为lame是音频的一个编码器。

代码地址在文末~~~~

为什么用到lame?

博主主要是用lamb对音频体积进行压缩,实际中,lame可以把一个9百多kb的文件,压缩到15k左右,能进一步优化应用的资源占用。

下面开始编译教程:

(一)复制文件到工程

官网下载lame的版本,博主这里使用的是3.99版本:
对应的下载链接为:lame3.99
下载后,打开,而我们要用到的目录是:lame-3.99.5\libmp3lame 下面的所有.h,.c文件。还有:lame-3.99.5\include目录下的lame.h头文件,截图如下:
lame.h头文件位置
lame编译文件位置

然后在AndroidStudio新建一个C的新项目,在把上面的文件复制到cpp文件夹里面,如下图:
编译文件复制到项目中的cpp下的文件目录,这里博主新建了一个lame名字的文件夹保存

(二)修改需要编译的文件

源文件修改

1.删除 fft.c 文件的 47 行的 include“vector/lame_intrin.h”
2.修改 set_get.h 文件的 24 行的 #include“lame.h”
3.将 util.h 文件的 574 行的”extern ieee754_float32_t fast_log2(ieee754_float32_t x);” 替换为 “extern float fast_log2(float x);”

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的最小版本号
cmake_minimum_required(VERSION 3.4.1)
#定义项目名称,可以不定义
#project(demo)
# 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.

#设置so库的输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})


#设置编译类型
#add_executable(demo demo.cpp) # 生成可执行文件
#生成动态共享库
add_library( # 设置编译成so库的名称
        lame-lib

        # 生成动态库或共享库,此处如果SHARED改为STATIC,其含义是生成静态库
        SHARED

        # 提供一个需要编译的源文件的相对路径(),native-lib.cpp就是需要编译的源文件
        native-lib.cpp
        lame/bitstream.c
        lame/encoder.c
        lame/fft.c
        lame/gain_analysis.c
        lame/id3tag.c
        lame/lame.c
        lame/mpglib_interface.c
        lame/newmdct.c
        lame/presets.c
        lame/psymodel.c
        lame/quantize.c
        lame/quantize_pvt.c
        lame/reservoir.c
        lame/set_get.c
        lame/tables.c
        lame/takehiro.c
        lame/util.c
        lame/vbrquantize.c
        lame/VbrTag.c
        lame/version.c
        )

#明确指定编译时需要编译哪些源文件
#add_library(demo demo.cpp test.cpp util.cpp)

#aux_source_directory(dir VAR) 发现一个目录下所有的源代码文件并将列表存储在一个变量中。
#例如:aux_source_directory(. SRC_LIST) # 搜索当前目录下的所有.cpp文件
#add_library(demo ${SRC_LIST})


# 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)

# 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需要链接的库
target_link_libraries( # Specifies the target library.目标库
        lame-lib

        # Links the target library to the log library 目标库需要链接的库,log-lib是上面find_library指定的变量名
        # included in the NDK.
        ${log-lib})
build.gradle文件修改

build.gradle文件修改地方

(三)编写native-lib.cpp文件,这里写获取版本号的代码

/**
 * 获取lame的版本号
 * */
extern "C"
JNIEXPORT jstring JNICALL
Java_com_north_light_liblame_LameUtils_getLameVersion(JNIEnv *env, jobject thiz) {
    return env->NewStringUTF(get_lame_version());
}

记得要导入lame.h依赖才可以生效

然后在java类里面声明定义好的C层方法:

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    private native String getLameVersion();

这样,项目就可以直接调用这个getLameVersion()方法来获取lame的版本号了。

最终运行截图:
运行截图

备注:
AndroidStudio需要配置配置ndk环境
复制编译文件时,切记找到相关目录的文件
CMakeLists.txt文件的编写,注意空格

代码地址

that‘s all------------------------------------------------------------------------------------

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值