上一节中编译了ffmpeg的库,这一节就那库来使用转码。
新建一个jni项目,目前as对于jni 有很好的支持,可以直接建立项目生成make 脚本等文件,不像eclipse,手动写mk等等。
生成的项目结构目录
cmake。配置添加
set(distribution_DIR /Users/xingweitong/androidProject/ffmpeg/app/libs/armeabi-v7a)
# 指定头文件目录
INCLUDE_DIRECTORIES(include)
add_library(avcodec-58
SHARED
IMPORTED)
set_target_properties(avcodec-58
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavcodec-58.so)
add_library(avdevice-58
SHARED
IMPORTED)
set_target_properties(avdevice-58
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavdevice-58.so)
add_library(avfilter-7
SHARED
IMPORTED)
set_target_properties(avfilter-7
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavfilter-7.so)
add_library(avformat-58
SHARED
IMPORTED)
set_target_properties(avformat-58
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavformat-58.so)
add_library(avutil-56
SHARED
IMPORTED)
set_target_properties(avutil-56
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavutil-56.so)
add_library(swresample-3
SHARED
IMPORTED)
set_target_properties(swresample-3
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswresample-3.so)
add_library(swscale-5
SHARED
IMPORTED)
set_target_properties(swscale-5
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswscale-5.so)
target_link_libraries(# Specifies the target library.
native-lib
avcodec-58
avdevice-58
avfilter-7
avformat-58
avutil-56
swresample-3
swscale-5
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
c++代码
#include
#include
extern "C" {
#include "libavcodec/avcodec.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ffmpeg_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello ="Hello from C++";
hello= avcodec_configuration();
return env->NewStringUTF(hello.c_str());
}
as。配置 添加
android{
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
}
ndk{
abiFilters'armeabi-v7a'
}
效果图