本文用到的FFmpeg for Android 的开发库,需要先在Ubuntu上完成编译,关于编译的过程参见我的上一篇文章。
Android Studio版本为Android Studio Dolphin | 2021.3.1 Patch 1
1.打开Android Studio,新建工程,选择Native C++
最小API版本须与FFmpeg库一致
2.在app\src\main目录下创建ffmpeg文件夹,将ffmpeg开发库解压到ffmpeg下
4.在app/build.gradle中添加如下语句
externalNativeBuild {
ndk{
abiFilters "armeabi-v7a"
}
}
5.将app\src\main\ffmpeg下的armv7-a文件夹改为armeabi-v7a。这一步很重要,否则后面找不到头文件和库。
6.编辑app\src\main\cpp\CMakeLists.txt
(1).设置库路径,添加链接库
(2).设置头文件目录
(3).设置具体的链接库文件
# 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.18.1)
# Declares and names the project.
project("ffmpegandroid2")
# 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.
add_library( # Sets the name of the library.
ffmpegandroid2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
#1.设置库路径,添加链接库
set(JNI_LIBS_DIR ${CMAKE_SOURCE_DIR}/../ffmpeg)
add_library(avutil
SHARED
IMPORTED)
set_target_properties(avutil
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavutil.so )
add_library(avresample
SHARED
IMPORTED )
set_target_properties(avresample
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavresample.so )
add_library(swresample
SHARED
IMPORTED )
set_target_properties(swresample
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libswresample.so )
add_library(swscale
SHARED
IMPORTED )
set_target_properties(swscale
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libswscale.so )
add_library(avcodec
SHARED
IMPORTED )
set_target_properties(avcodec
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavcodec.so )
add_library(avformat
SHARED
IMPORTED )
set_target_properties(avformat
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavformat.so )
add_library(avfilter
SHARED
IMPORTED )
set_target_properties(avfilter
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavfilter.so )
add_library(avdevice
SHARED
IMPORTED )
set_target_properties(avdevice
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/lib/libavdevice.so )
#2.设置头文件目录
include_directories(${JNI_LIBS_DIR}/${ANDROID_ABI}/include)
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.
#3.设置具体的链接库文件
target_link_libraries( # Specifies the target library.
ffmpegandroid2
avutil
swresample
swscale
avcodec
avformat
avfilter
# Links the target library to the log library
# included in the NDK.
${log-lib})
7.打开app\src\main\cpp\native-lib.cpp,添加代码
extern "C"{
#include "libavcodec/avcodec.h"
}
...
return env->NewStringUTF(av_version_info());
8.点击右上角【同步】(大像)按钮,连接安卓手机点击【运行】按钮开始运行程序
手机上看ffmpeg版本号,说明ffmpeg运行成功。