今天給你們介紹把上次編譯出來的ffmpeg文件集成到Android中。本文使用的cmake構建的ndk環境。java
經過上篇文章Android-ffmpeg編譯so文件,相信你們已經編譯出來ffmpeg的頭文件和so文件了。android
把咱們當前的項目經過cmake構建一個NDK環境出來bash
android{
...
defaultConfig{
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
abiFilters "armeabi" #我當前編譯的是arm版本,因此此處指填寫arm
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
複製代碼
native-lib.cpp
,如圖所示CMakeLists.txt
文件
# 設置最小使用版本
cmake_minimum_required(VERSION 3.4.1)
# 添加本地so庫 native-lib:這個是聲明引用so庫的名稱 SHARED:表示共享so庫文件
# 構建so庫的源文件
add_library(
native-lib
SHARED
src/main/cpp/native-lib.cpp
)
# 使用系統ndk 提供的庫,如 log庫
# log-lib 這個指定的是在NDK庫中每一個類型的庫會存放一個特定的位置,而log庫存放
# 在log-lib中
# log 指定使用log庫
find_library(
log-lib
log
)
#----------------------ffmpeg的庫文件------------
# 加載頭文件
include_directories(src/main/cpp/include)
# 加載avcodec-57庫
add_library( avcodec-57
SHARED
IMPORTED)
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavcodec-57.so)
add_library( avdevice-57
SHARED
IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavdevice-57.so)
add_library( avfilter-6
SHARED
IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavfilter-6.so)
add_library( avformat-57
SHARED
IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavformat-57.so)
add_library( avutil-55
SHARED
IMPORTED)
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavutil-55.so)
add_library( swresample-2
SHARED
IMPORTED)
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libswresample-2.so)
add_library( swscale-4
SHARED
IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libswscale-4.so)
#----------------------end-----------------------
# 若是你本地的庫(native-lib)想要調用log庫的方法,
# 那麼就須要配置這個屬性,意思是把NDK庫關聯到本地庫。
# 第一個參數表示本地的庫 native-lib 要調用到log庫的方法,即要被關聯的庫名稱,log-lib 要關聯的庫名稱
target_link_libraries(
native-lib
#ffmpeg------start----------
avcodec-57
avdevice-57
avfilter-6
avformat-57
avutil-55
swresample-2
swscale-4
#ffmpeg------end------------
${log-lib}
)
複製代碼
native-lib
庫public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
text.setText(helloNDK("你好 C++ \n"));
}
public native String helloNDK(String msg);
}
複製代碼
#include <jni.h>
#include <android/log.h>
extern "C"{
#include <libavformat/avformat.h>
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ffmpeg_1test_MainActivity_helloNDK(JNIEnv* env,jobject,jstring msg){
char* chello = (char *) env->GetStringUTFChars(msg, JNI_FALSE);
__android_log_print(ANDROID_LOG_ERROR,"tag","c : %s",chello);#log日誌打印
__android_log_print(ANDROID_LOG_ERROR,"tag","編碼器配置: %s",avcodec_configuration());#log日誌打印
char * newstr = strcat(chello,avcodec_configuration());#將java傳過來的字符串和編碼器配置信息拼接起來並返回去
return env->NewStringUTF(newstr);
}
複製代碼