基础调用
1)编译安卓需要的so,请参考
2)将so引入安卓中使用
在module的src/main/下创建cpp和jniLibs模块,jniLibs中存放so文件
3)cpp中进行cmake配置
将ffmpeg的头文件拷贝到src/main/cpp下
配置cmakelist
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("ffmpeg")
#设置FFmpeg头文件的路径,共享库,include里面h文件调用其他h文件用到的
include_directories(
include#因为和CMakeLists.txt在同一级,所以直接写include
)
add_library(ffmpeg
SHARED
ffmpeg_util.cpp)
find_library(
log-lib
log)
add_library(avcodec
SHARED
IMPORTED)
#给avcodec这个变量赋值
set_target_properties(avcodec
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavcodec.so)
add_library(avdevice
SHARED
IMPORTED)
set_target_properties(avdevice
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavdevice.so)
add_library(avformat
SHARED
IMPORTED)
set_target_properties(avformat
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavformat.so)
add_library(avutil
SHARED
IMPORTED)
set_target_properties(avutil
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavutil.so)
add_library(swresample
SHARED
IMPORTED)
set_target_properties(swresample
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libswresample.so)
add_library(swscale
SHARED
IMPORTED)
set_target_properties(swscale
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libswscale.so)
add_library(avfilter
SHARED
IMPORTED)
set_target_properties(avfilter
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavfilter.so)
target_link_libraries(ffmpeg
${log-lib}
avcodec swresample avutil)
4) 编写jni测试代码
#include <android/log.h>
#include <jni.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
/*Include ffmpeg header file*/
#include "libavcodec/avcodec.h"
#ifdef __cplusplus
}
#endif
extern "C" JNIEXPORT jstring JNICALL
Java_com_cd_lib_ffmpeg_FfmpegUtil_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
return env->NewStringUTF(avcodec_configuration());
}
根据jni规则编码java
public class FfmpegUtil {
// Used to load the 'ffmpeg' library on application startup.
static {
System.loadLibrary("ffmpeg");
}
/**
* A native method that is implemented by the 'ffmpeg' native library,
* which is packaged with this application.
*/
public static native String stringFromJNI();
}
5) 配置gradle相关位置
android {
...
defaultConfig {
...
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
...
}