欢迎加入技术谈论群: 714476794
1、播放视频
1.1、新建Android项目
笔者用的AndroidStudio 3.6.1版本,新建项目选Native C++,其他版本可能不太一样,语言这里选择的是Kotlin
1.2、配置项目
这里我们只需要armeabi-v7架构的FFmpeg,如需要其他架构,自行编译,将编译好的armv7/lib下的so复制到app/libs/armeabi-v7a,笔者一般都习惯把第三方so库放到module/libs下面,这里需要注意在module的build.gradle加入配置
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
然后复制include文件夹到cpp文件夹下,复制到哪里都无所谓,只要在CMakeLists.txt配置对就行。
1.3、编写CMakeLists.txt
如果AndroidStudio创建项目没有自动生成的需要手动新建
# 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.
#设置生成的动态库目录 生成的so在当前项目不需要手动拷贝到libs或jniLibs,生成apk会自动引入,这里生成的可以供其他项目使用
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
#设置jni库目录
set(JNI_LIBS_DIR ${CMAKE_SOURCE_DIR}/../../../libs)
add_library( video
SHARED
video.cpp)
find_library( log-lib
log )
add_library( avcodec SHARED IMPORTED)
set_target_properties(
avcodec
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavcodec.so)
add_library(avdevice SHARED IMPORTED)
set_target_properties(
avdevice
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavdevice.so)
add_library(avfilter SHARED IMPORTED)
set_target_properties(
avfilter
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavfilter.so)
add_library(avformat SHARED IMPORTED)
set_target_properties(
avformat
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(
avutil
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavutil.so)
add_library(postproc SHARED IMPORTED)
set_target_properties(
postproc
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libpostproc.so)
add_library(swresample SHARED IMPORTED)
set_target_properties(
swresample
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libswresample.so)
add_library(swscale SHARED IMPORTED)
set_target_properties(
swscale
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libswscale.so)
#添加头文件
include_directories(${CMAKE_SOURCE_DIR}/include)
target_link_libraries(
video
avcodec
avdevice
avfilter
avformat
avutil
postproc
swresample
swscale
android
${log-lib} )
1.4 配置build.gradle
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
abiFilters "armeabi-v7a"
}
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"//CMakeLists.txt文件路径
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
1.5、编写C代码
在src/main/cpp新建video.cpp
参考文章https://blog.youkuaiyun.com/johanman/article/details/83091706
//
// Created by k on 2020-05-13.
//
#include <jni.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/log.h>
extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
}
// Android 打印 Log
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR, "FFmpeg_log", FORMAT, ##__VA_ARGS__);
extern "C"
JNIEXPORT void JNICALL
Java_com_mrkzs_android_ffmpegplayvideo_MainActivity_playVideo(JNIEnv *env, jobject instance, jstring source, jobject surface) {
// 记录结果
int result;
// R1 Java String -> C String
const char *path = env->GetStringUTFChars(source