首先,前提是將FFmpeg編譯生成庫,具體的FFmpeg編譯暫不做分享(涉及l(fā)ibfdk_aac,x264文章待續(xù)......)。
Android Studio2.2已經(jīng)可以通過cmake建立JNI項目,而且native debug也很方便,于是通過cmake移植FFmpeg至android平臺。
- 新建android項目,在src/main/目錄下,創(chuàng)建JniLibs目錄。并將動態(tài)庫添加到此目錄下,同時將FFMpeg編譯生成的include頭文件拷貝到cpp目錄下.

image1.png
-
JNI頭文件生成
編寫native方法,alt+enter 生成頭文件,實現(xiàn)簡單調(diào)用
image2.png -
在JNI中涉及一以C與C++的混合調(diào)用,不要忘記添加extern "C"{},否則編譯會報錯
image3.png -
因為只編譯armeabi-v7a平臺的動態(tài)庫,在CMakelists.txt文件中,通過配置文件添加依賴庫,注意build.gradle中不要忘記添加ndk配置,否則會鏈接失敗。
image4.png 以下為CMakelists.txt文件中的詳細配置
set(include-headers ${CMAKE_SOURCE_DIR}/src/main/cpp/include) //定義變量,設(shè)置頭文件路徑
include_directories(${include-headers})//引用變量包含頭文件,否則編譯過程中找不到頭文件
add_library(avcodec SHARED IMPORTED)//添加動態(tài)庫
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavcodec-57.so)//動態(tài)庫的路徑
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 )//通過源文件的形式,加入動態(tài)庫
#以上添加依賴的動態(tài)庫和源文件
#鏈接動態(tài)庫
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,跑起......

screen_shot.png


