ffmpeg编译好了对应android平台的文件之后就可以移植到anroid上使用了。
一、在android studio中创建一个支持C++的项目
android studio sdk tools下载cmake、ndk。
将编译好的ffmpeg头文件跟so文件拷贝到android项目对应目录下。
二、配置相关信息
在CMakeLists.txt里面配置
cmake_minimum_required(VERSION 3.4.1)
include_directories(../../../includes)
#set ffmpeg
set(ffmpeg ${CMAKE_SOURCE_DIR}/../../../jniLibs/${ANDROID_ABI})
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${ffmpeg}/libavcodec.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).
native-lib.cpp )
##you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
avcodec
# Links the target library to the log library
# included in the NDK.
${log-lib} )
在build.gradle中配置,ndk设置调用armeabi-v7a,设置jniLib目录。
注意,这里要加上 pickFirst ‘lib/armeabi-v7a/libavcodec.so’,
否则编译时会报
More than one file was found with OS independent path ‘lib/armeabi-v7a/libavcodec.so’
三、调用ffmpeg
在native-lib.cpp中 调用 avcodec configuration
#include <jni.h>
#include <string>
#include <android/log.h>
#define LOG_TAG "C_TAG"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG ,__VA_ARGS__) // 定义LOGI类型
extern "C" {
#include <libavcodec/avcodec.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ffmpeg_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
//std::string hello = "avcodec configuration info ";
std::string config = avcodec_configuration();
LOGI("configuration %s\n",config.c_str());
return env->NewStringUTF(config.c_str());
}
打印出配置信息,表示编译的ffmpeg在android调用成功。