Android Studio引入FFmpeg的方法
新建C++工程
新建
兩個externalNativeBuild
一個sourceSets(指定so路徑)
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'armeabi-v7a'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
}
...
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
復制so和include文件

編寫CMakeLists.txt
以下是默認值
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib
SHARED
native-lib.cpp
#nativ-lib2.cpp 如果有其他cpp文件可以一并打包到native-lib中)
#查找系統(tǒng)的log庫,并賦值給變量log-lib
find_library(
log-lib
log)
#將上面log-lib變量里的庫連接到native-lib中
target_link_libraries(
native-lib
${log-lib})
CMakeLists中添加FFmpeg頭文件路徑
#設置FFmpeg頭文件的路徑 include_directories( include#因為和CMakeLists.txt在同一級,所以直接寫include )
CMakeLists中添加libavcodec.so
#定義一個變量avcodec
add_library(
avcodec
SHARED
IMPORTED
)
#給avcodec這個變量賦值
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec.so)
#將avcodec混合編譯到native-lib中
target_link_libraries(
native-lib
${log-lib}
avcodec
)
CMakeLists中添加全部的so
cmake_minimum_required(VERSION 3.4.1)
#設置FFmpeg頭文件的路徑
include_directories(
include#因為和CMakeLists.txt在同一級,所以直接寫include
)
add_library(native-lib
SHARED
native-lib.cpp)
find_library(
log-lib
log)
#1.定義一個變量avcodec
add_library(
avcodec
SHARED
IMPORTED
)
#給avcodec這個變量賦值
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec-57.so)
#2.
add_library(
avdevice
SHARED
IMPORTED
)
set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavdevice-57.so)
#3.
add_library(
avfilter
SHARED
IMPORTED
)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavfilter-6.so)
#4.
add_library(
avformat
SHARED
IMPORTED
)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavformat-57.so)
#5.
add_library(
avutil
SHARED
IMPORTED
)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavutil-55.so)
#6.
add_library(
postproc
SHARED
IMPORTED
)
set_target_properties(postproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libpostproc-54.so)
#7.
add_library(
swresample
SHARED
IMPORTED
)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswresample-2.so)
#8.
add_library(
swscale
SHARED
IMPORTED
)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswscale-4.so)
#將avcodec混合編譯到native-lib中
target_link_libraries(
native-lib
${log-lib}
avcodec#1
avdevice#2
avfilter#3
avformat#4
avutil#5
postproc#6
swresample#7
swscale#8
)
編寫測試函數(shù)
#include <jni.h>
#include <string>
extern "C" {
#include "libavcodec/avcodec.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myffmpegcmd_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(avcodec_configuration());
}

總結
到此這篇關于Android Studio引入FFmpeg的文章就介紹到這了,更多相關Android Studio引入FFmpeg內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android Accessibility 輔助功能簡單介紹
這篇文章主要介紹了Android Accessibility 輔助功能簡單介紹的相關資料,文字轉(zhuǎn)語音,觸覺反饋,手勢操作,軌跡球和手柄操作,需要的朋友可以參考下2016-11-11
Android開發(fā)中RecyclerView組件使用的一些進階技講解
RecyclerView是Android 5.0以來新加入的一個組件,基本上全面優(yōu)于ListView,這里我們將來關注Android開發(fā)中RecyclerView組件使用的一些進階技講解:2016-06-06
android使用surfaceview+MediaPlayer播放視頻
這篇文章主要為大家詳細介紹了android使用surfaceview+MediaPlayer播放視頻,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

