记录一个AndroidStudio新建FFmpeg项目的流程。
=================================================
目录
1、在AndroidStudio中新建一个FFmpegtest项目。勾选Include C++ support
2、如果遇到 Configure build 这种错误。可以检查一下 NDK路径 是否配置。
1、将AndroidStudio工程切换到 Project 这种模式
2、在 libs 目录下,新建armeabi 和 include 文件夹,里面分别存放编译得到的 .so文件和 头文件。
3、更改 CMakeLists.txt 文件中的内容,CMakeLists.txt 文件位置如下
一、配置默认支持 C++ 的工程
1、在AndroidStudio中新建一个FFmpegtest项目。勾选Include C++ support
选择一个版本
此时,新建工程完毕。
2、如果遇到 Configure build 这种错误。可以检查一下 NDK路径 是否配置。
点击 File - Project Structure
可以点击选择自己电脑中下载的 NDK。我这边用的是 android-ndk-r13b
点击OK之后,等待AndroidStudio一会,一个支持C++的工程就建好了。
二、加入编译的.so库
我这里用的.so库是通过ubuntu编译的,具体步骤可见另一篇博客:
ubuntu编译ffmpeg 生成.so文件,点我
1、将AndroidStudio工程切换到 Project 这种模式
2、在 libs 目录下,新建armeabi 和 include 文件夹,里面分别存放编译得到的 .so文件和 头文件。
3、更改 CMakeLists.txt 文件中的内容,CMakeLists.txt 文件位置如下
将其中所有内容删除用下面配置代替:
cmake_minimum_required(VERSION 3.4.1)
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).
src/main/cpp/native-lib.cpp )
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( native-lib
avfilter-6
avcodec-57
avdevice-57
avformat-57
avutil-55
postproc-54
swresample-2
swscale-4
${log-lib} )
include_directories(libs/include)
set(DIR ../../../../libs)
add_library(avcodec-57
SHARED
IMPORTED)
set_target_properties(avcodec-57
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavcodec-57.so)
add_library(avdevice-57
SHARED
IMPORTED)
set_target_properties(avdevice-57
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavdevice-57.so)
add_library(avformat-57
SHARED
IMPORTED)
set_target_properties(avformat-57
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavformat-57.so)
add_library(avutil-55
SHARED
IMPORTED)
set_target_properties(avutil-55
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavutil-55.so)
add_library(postproc-54
SHARED
IMPORTED)
set_target_properties(postproc-54
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libpostproc-54.so)
add_library(swresample-2
SHARED
IMPORTED)
set_target_properties(swresample-2
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswresample-2.so)
add_library(swscale-4
SHARED
IMPORTED)
set_target_properties(swscale-4
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswscale-4.so)
add_library(avfilter-6
SHARED
IMPORTED)
set_target_properties(avfilter-6
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavfilter-6.so)
需要注意:配置中涉及到
add_library(postproc-54
SHARED
IMPORTED)
set_target_properties(postproc-54
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libpostproc-54.so)
..........
avfilter-6
avcodec-57
avdevice-57
avformat-57
avutil-55
postproc-54
swresample-2
swscale-4
后面的序号需要和导入的 .so 库的对应。
4、配置build.gradle文件
build.gradle文件在如下目录
添加如下配置信息
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
externalNativeBuild {
cmake {
abiFilters 'armeabi'
}
}
添加位置在:
5、配置 native-lib.cpp 文件
native-lib.cpp 文件在
参考如下代码:
需要注意
Java_com_example_chy_ffmpegtest_MainActivity_stringFromJNI
命名要和你的工程配置相对应。
#include <jni.h>
#include <string>
extern "C"
{
#include "libavcodec/avcodec.h"
JNIEXPORT jstring JNICALL
Java_com_example_chy_ffmpegtest_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = avcodec_configuration();
return env->NewStringUTF(hello.c_str());
}
};
其中
#include "libavcodec/avcodec.h" 是为了引入头文件
avcodec_configuration(); 是返回构建信息。
三、结果
运行工程,接上真机,结果如下图所示:
OK,FFMpeg 第一个工程启动成功,欢迎来到 FFMpeg 的世界!