上一篇文章我们讲过了ffmpeg的编译,这一次我们讲一下在使用ffmpeg的时候遇到的问题
1.打开AndroidStudio创建一个C++项目,选择C++11
注意:新版本的AS把CMakeLists文件放在了cpp目录下,我们需要把这个文件放到src同级别目录下,同时修改一下app下的build.gradle 引用CMakeLists的配置
build.gradle配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.xxxx.ffmepg"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'x86_64'//这里因为是mac电脑模拟器的cpu架构选的是x86_64
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"//这个地方要修改默认生成的src/main/cpp/CMakeLists.txt
version "3.6.0"//将默认的高版本调低,避免日志乱码
}
}
sourceSets{
main{
jniLibs.srcDirs = ["src/main/jniLibs"]
}
}
}
将上一篇ffmpeg中编译出得x86_64和include文件分别放到cpp和jniLibs目录下
配置CMakeList.txt
# 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.
#remove some build warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations ")
#FFMpeg配置
#FFmpeg配置目录
#这里就是为啥要将CMakeList放到src同级目录下的原因,使用便捷
set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
#配置编译的头文件
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
# 编解码(最重要的库)
add_library(
avcodec-57
SHARED
IMPORTED)
set_target_properties(
avcodec-57
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavcodec-57.so)
# 滤镜特效处理库
add_library(
avfilter-6
SHARED
IMPORTED)
set_target_properties(
avfilter-6
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavfilter-6.so)
# 封装格式处理库
add_library(
avformat-57
SHARED
IMPORTED)
set_target_properties(
avformat-57
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavformat-57.so)
# 工具库(大部分库都需要这个库的支持)
add_library(
avutil-55
SHARED
IMPORTED)
set_target_properties(
avutil-55
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavutil-55.so)
# 音频采样数据格式转换库
add_library(
swresample-2
SHARED
IMPORTED)
set_target_properties(
swresample-2
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libswresample-2.so)
# 视频像素数据格式转换
add_library(
swscale-4
SHARED
IMPORTED)
set_target_properties(
swscale-4
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libswscale-4.so)
#设置编译忽略过时方法使用导致的编译不通过
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wno-deprecated-declarations")
#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
#if(CMAKE_COMPILER_IS_GNUCXX)
# set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
# message(STATUS "optional:-std=c++11")
#endif(CMAKE_COMPILER_IS_GNUCXX)
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).
${CMAKE_SOURCE_DIR}/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 )