您好,登錄后才能下訂單哦!
小編給大家分享一下Android Studio如何引入FFmpeg的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
新建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中) #查找系統的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 )
編寫測試函數
#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的方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。