首先,前提是将FFmpeg编译生成库,具体的FFmpeg编译暂不做分享(涉及libfdk_aac,x264文章待续......)。
Android Studio2.2已经可以通过cmake建立JNI项目,而且native debug也很方便,于是通过cmake移植FFmpeg至android平台。
- 新建android项目,在src/main/目录下,创建JniLibs目录。并将动态库添加到此目录下,同时将FFMpeg编译生成的include头文件拷贝到cpp目录下.
-
JNI头文件生成
编写native方法,alt+enter 生成头文件,实现简单调用
-
在JNI中涉及一以C与C++的混合调用,不要忘记添加extern "C"{},否则编译会报错
-
因为只编译armeabi-v7a平台的动态库,在CMakelists.txt文件中,通过配置文件添加依赖库,注意build.gradle中不要忘记添加ndk配置,否则会链接失败。
以下为CMakelists.txt文件中的详细配置
set(include-headers ${CMAKE_SOURCE_DIR}/src/main/cpp/include) //定义变量,设置头文件路径
include_directories(${include-headers})//引用变量包含头文件,否则编译过程中找不到头文件
add_library(avcodec SHARED IMPORTED)//添加动态库
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavcodec-57.so)//动态库的路径
add_library(avdevice SHARED IMPORTED)
set_target_properties( avdevice
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavdevice-57.so )
add_library(avfilter SHARED IMPORTED)
set_target_properties( avfilter
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavfilter-6.so )
add_library(avformat SHARED IMPORTED)
set_target_properties( avformat
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavformat-57.so )
add_library(avutil SHARED IMPORTED)
set_target_properties( avutil
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavutil-55.so )
add_library(postproc SHARED IMPORTED)
set_target_properties( postproc
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libpostproc-54.so )
add_library(swresample SHARED IMPORTED)
set_target_properties( swresample
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswresample-2.so )
add_library(swscale SHARED IMPORTED)
set_target_properties( swscale
PROPERTIES
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswscale-4.so )
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )//通过源文件的形式,加入动态库
#以上添加依赖的动态库和源文件
#链接动态库
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
avcodec
avdevice
avfilter
avformat
avutil
postproc
swresample
swscale
)
ok,跑起......