新建项目
- 首先新建一个工程,并且勾选 Include C++ Support 即可得到一个基于CMake的模板工程。
- 将编译FFmpeg生成的头文件和动态库分别拷贝到app/src/main/jniLibs app/src/main/cpp/include目录下。
配置build.gradle
在app目录下的build.gradle文件中,android节点下的defaultConfig节点下添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| defaultConfig { applicationId "com.project.ffmpegplayer" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "-frtti -fexceptions" abiFilters 'armeabi-v7a' } }
sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] } } }
|
配置CMakeLists.txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| cmake_minimum_required(VERSION 3.4.1) include_directories(include) add_library( native-lib SHARED native-lib.cpp )
#添加libavcodec.so add_library( avcodec SHARED IMPORTED) set_target_properties( avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec.so)
#添加libavdevice.so add_library(avdevice SHARED IMPORTED) set_target_properties( avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavdevice.so)
add_library( avfilter SHARED IMPORTED) set_target_properties( avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavfilter.so)
add_library( avformat SHARED IMPORTED) set_target_properties( avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavformat.so)
add_library( avutil SHARED IMPORTED) set_target_properties( avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavutil.so)
add_library( swresample SHARED IMPORTED) set_target_properties( swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswresample.so)
add_library( swscale SHARED IMPORTED) set_target_properties( swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswscale.so)
find_library( log-lib log ) target_link_libraries( native-lib avcodec avdevice avfilter avformat avutil swresample swscale ${log-lib} )
|
编写测试代码(头文件一定要用extern 包含住)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <jni.h> #include <string> extern "C" { #include "include/libavcodec/avcodec.h" #include "include/libavformat/avformat.h" }
extern "C" JNIEXPORT jstring JNICALL Java_com_project_ffmpegplayer_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; avformat_version(); return env->NewStringUTF(avcodec_configuration()); }
|
参考
- 码牛学院https://maniu.ke.qq.com/