上一篇文章介绍了如何在Mac平台下编译FFMPEG,这篇主要介绍如何将编译后的so文件导入Android项目中,
-
项目文件目录结构
src/main/cpp/include包下的文件可以从FFMPEG编译后的任意CPU架构目录下直接拷贝到cpp目录下即可
-
src/main/jniLibs/包下的文件可以从FFMPEG编译后的对应的CPU架构Lib包下拷贝,只需要拷贝以带版本号的so文件即可,如:libavcodec-57.so
-
配置项目的build.gradle文件
android { ..... externalNativeBuild { cmake { path "CMakeLists.txt" } } sourceSets{ main{ jniLibs.srcDirs = ["src/main/jniLibs"] } } }
-
配置CMakeLists.txt文件
# Cmake版本号 cmake_minimum_required(VERSION 3.4.1) #项目自动生成的代码 add_library( native-lib SHARED src/main/cpp/native-lib.cpp) #复用NDK中一些基础的库,如输出log等 find_library( log-lib log) #导入FFMPEG动态库: #1.导入FFMEPG头文件夹路径 include_directories(src/main/cpp/include) #2. 添加动态库: 库名 SHARED IMPORTED add_library(avcodec-57 SHARED IMPORTED) #3. 设置动态库路径:${CMAKE_SOURCE_DIR}表示CmakeList文件目录 ${ANDROID_ABI}表示系统CPU架构 set_target_properties(avcodec-57 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec-57.so) #其他so文件按照第2、3步重复操作即可,只需要替换对应的so库名称即可 add_library( avdevice-57 SHARED IMPORTED) set_target_properties( avdevice-57 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavdevice-57.so) add_library( avfilter-6 SHARED IMPORTED) set_target_properties( avfilter-6 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavfilter-6.so) add_library( avformat-57 SHARED IMPORTED) set_target_properties( avformat-57 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavformat-57.so) add_library( avutil-55 SHARED IMPORTED) set_target_properties( avutil-55 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavutil-55.so) add_library( postproc-54 SHARED IMPORTED) set_target_properties( postproc-54 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpostproc-54.so) add_library( swresample-2 SHARED IMPORTED) set_target_properties( swresample-2 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswresample-2.so) add_library( swscale-4 SHARED IMPORTED) set_target_properties( swscale-4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswscale-4.so) #4. 链接动态库 target_link_libraries( native-lib ${log-lib} # FFMPEG相关的类库 avcodec-57 avdevice-57 avfilter-6 avformat-57 avutil-55 postproc-54 swresample-2 swscale-4)
-
编写C++测试代码:
#include <jni.h> #include <string> #include <android/log.h> //由于调用时是用C语言写的,所以也要用extern声明是C,否则编译时会报错 extern "C"{ #include <libavformat/avformat.h> } //定义打印Log方法 #define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"MyPlayer",FORMAT,##__VA_ARGS__); extern "C" JNIEXPORT jstring JNICALL Java_cn_rongrtc_mylibrary_Demo_stringFromJNI( JNIEnv *env, jobject /* this */) { //注册所有的解码器 av_register_all(); //获取所有的解码器 AVCodec *c_temp = av_codec_next(NULL); //遍历解码器并打印解码器名称 while (c_temp != NULL) { switch (c_temp->type) { case AVMEDIA_TYPE_VIDEO: LOGI("[Video]:%s", c_temp->name); break; case AVMEDIA_TYPE_AUDIO: LOGI("[Audio]:%s", c_temp->name); break; default: LOGI("[Other]:%s", c_temp->name); break; } c_temp = c_temp->next; } std::string hello = "Hello from Demo C++"; return env->NewStringUTF(hello.c_str()); }
-
编写Java层代码
public class Demo { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); System.loadLibrary("avcodec-57"); System.loadLibrary("avdevice-57"); System.loadLibrary("avfilter-6"); System.loadLibrary("avformat-57"); System.loadLibrary("avutil-55"); System.loadLibrary("postproc-54"); System.loadLibrary("swresample-2"); System.loadLibrary("swscale-4"); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public static native String stringFromJNI(); }
遇到的问题
-
同步代码或编译时如果有类似的报错:
Error while executing process /Users/www/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake with arguments {--build /Users/www/codeSamples/ffmpeg/MusicPlayer/mylibrary/.externalNativeBuild/cmake/debug/arm64-v8a --target native-lib}
ninja: error: '../../../../src/main/jniLibs/arm64-v8a/libavcodec-57.so', needed by '../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so', missing and no known rule to make it
-
如果出现类似报错则有可能是你的so库不支持你手机CPU架构,你需要编译一份对应的CPU架构so库并拷贝到jniLibs目录下即可。具体如何编译可以参考上一篇文章Mac平台下编译FFMPEG