AndroidStudio版本3.2

<>1.创建Android项目
<>1.1打开AndroidStudio,创建一个名为FirstFfmpeg的项目,勾选C++支持 <>1.2 minimun
SDK选择5.0,4.0或者4.1都可以,根据自身项目需求 <>1.3 C++ Standard 选择C++11 <>1.4等待Android项目创建完成
为了方便查看Android文件目录,可以将Android视图结构切换为Project


<>2.导入已经编译好的ffmpeg头文件和库文件
<>2.1导入ffmpeg库头文件:复制编译好的ffmpeg的动态库中的头文件到app目录下 <>
2.2导入ffmpeg动态库文件:在app/libs下新建armeabi-v7a文件夹,将动态库复制到该目录下
<>3.编写CMakeLists.txt文件,Android会默认生成这个文件,我们只需要修改即可,如下,添加的地方都有详细的注释,相信都能看得懂
# 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.4.1) # Creates and names a library, sets it as
either STATIC # or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you. # Gradle
automatically packages shared libraries with your APK.
#添加头文件目录,相对于CMakeLists.txt也就是本文件的路径,从目录
#结构中可以看出,CMakeLists.txt和include目录是在同一级目录 include_directories(include)
#设置ffmpeg库所在路径的变量 FF为表示路径的变量,
#CMAKE_CURRENT_SOURCE_DIR为CMake内置变量,表示CMakeLists所在的目录 #ANDROID_ABI
CPU架构类型,如果gradle文件没有设置过滤, #则会表示所有的架构,arm-v7a arm-v8a x86 x86_64
#所以FF=CMAKE_CURRENT_SOURCE_DIR/libs/armeabi-v7a set(FF
${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}) #添加avcodec SHARED表示动态库
IMPORTED表示从外部导入 add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)
#avformat add_library(avformat SHARED IMPORTED) set_target_properties(avformat
PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.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). src/main/cpp/native-lib.cpp) #
Searches for a specified prebuilt library and stores the path as a # variable.
Because CMake includes system libraries in the search path by # default, you
only need to specify the name of the public NDK library # 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) # Specifies
libraries CMake should link to your target library. You # can link multiple
libraries, such as libraries you define in this # build script, prebuilt
third-party libraries, or system libraries. #将添加进来的库avcodec
avformat链接到native-lib库中, #因为我们最终只会生成一个libnative-lib.so库
#这个库如果没有指定路径,那么可以在app/build/cmake/debug/obj中找到 target_link_libraries( #
Specifies the target library. native-lib avcodec avformat # Links the target
library to the log library # included in the NDK. ${log-lib})
<>4在native-lib.cpp中添加avcodec.h头文件,调用avcodec_configuration()打印ffmpeg
configure参数
#include <jni.h> #include <string>
//ffmpeg是基于C语言的库,所以在使用ffmpeg的头文件的时候,需要用extern来标记 extern "C" { #include
<libavcodec/avcodec.h> } extern "C" JNIEXPORT jstring JNICALL
Java_c_lucas_firstffmpeg_MainActivity_stringFromJNI( JNIEnv *env, jobject /*
this */) { std::string hello = "Hello from C++";
//avcodec_configuration()返回ffmpeg配置参数 hello += avcodec_configuration(); return
env->NewStringUTF(hello.c_str()); }
<>5.修改app下build.gradle,主要是设置只编译armeabi-v7a架构,设置ffmpeg动态库目录
defaultConfig { applicationId "c.lucas.firstffmpeg" minSdkVersion 21
targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake {
cppFlags "-std=c++11" } //只编译armeabi-v7a架构 ndk { abiFilters "armeabi-v7a" } }
//设置ffmpeg动态库目录 相对于app下的build.gradle文件目录 sourceSets{ main{
jniLibs.srcDirs=['libs'] } } }
<>6.最后编译运行,可以在手机屏幕上看到ffmpeg的编译配置信息。


友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信