(二)Android引入ffmpeg编译后的so库

手机游戏开发者 2024-9-13 14:35:00 82 0 来自 中国
怎样编译ffmepg的动态链接库,可以看这里:https://www.jianshu.com/p/7dfd64f906e5。
1、新建项目,将编译好的ffmpeg库,拷贝到项目标libs文件夹中。

2、在src/main路径下,创建cpp文件夹,将编译好的头文件放到cpp文件夹下,并创建一个native-lib.cpp文件(暂时不必要在内里写代码)

3、创建CMakeLists.txt文件

3.png 而且在该文件内,添加和设置上面我们引入的库文件。
cmake_minimum_required(VERSION 3.4.1)# # 界说LIBS_DIRset(LIBS_DIR ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI})# 添加头文件目次include_directories(src/main/cpp/include/)#  添加ffmpeg相干的so库# 添加库——外部引入的库# 库名称:avcodec(不必要包罗前缀lib)# 库范例:SHARED,表示动态库,后缀为.so(假如是STATIC,则表示静态库,后缀为.a)# IMPORTED表明是外部引入的库add_library( avcodec        SHARED        IMPORTED)# 设置目标属性# 设置avcodec目标库的IMPORTED_LOCATION属性,用于分析引入库的位置# 还可以设置其他属性,格式:PROPERTIES key valueset_target_properties( avcodec        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libavcodec.so)add_library( avfilter        SHARED        IMPORTED)set_target_properties( avfilter        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libavfilter.so)add_library( avformat        SHARED        IMPORTED)set_target_properties( avformat        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libavformat.so)add_library( avutil        SHARED        IMPORTED)set_target_properties( avutil        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libavutil.so)add_library( swresample        SHARED        IMPORTED)set_target_properties( swresample        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libswresample.so)add_library( swscale        SHARED        IMPORTED)set_target_properties( swscale        PROPERTIES IMPORTED_LOCATION        ${LIBS_DIR}/libswscale.so)# 查找代码中使用到的体系库find_library(        log-lib        log )# 设置目标so库编译信息add_library(        native-lib        SHARED        src/main/cpp/native-lib.cpp)# 指定编译目标库时,cmake要链接的库target_link_libraries(        # 指定目标库,native-lib 是在上面 add_library 中设置的目标库        native-lib        # 4. 毗连 FFmpeg 相干的库        avutil        swresample        avcodec        avfilter        swscale        avformat        # Links the target library to the log library        # included in the NDK.        ${log-lib} )4、在app的build.gradle中添加ffmepg的设置项。

android {    compileSdk 31    defaultConfig {       ...省略...        //设置cmake编译选项和支持的CPU指令集        externalNativeBuild {            cmake {                cppFlags "-frtti -fexceptions -Wno-deprecated-declarations"            }            ndk{                abiFilters "armeabi-v7a"            }        }    }    //设置CMakeLists.txt文件的路径    externalNativeBuild {        cmake {            path "CMakeLists.txt"        }    }    ...省略...}5、在你必要使用的地方调用

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        val tvName:TextView = findViewById(R.id.tvName)        tvName.text = getConfiguration()    }    private external fun ffmpegVersion():String    private external fun getConfiguration():String    companion object{        init {            System.loadLibrary("native-lib")        }    }}这里用简朴的activity来举例,起首我们是在伴生对象里初始化“native-lib”,然后界说一个external方法,方法名随意,由于没有实现,以是会报错,但是只必要根据Androidstudio的提示,使用快捷键就能主动在native-lib.cpp文件中创建对应的方法,我们只必要做的,就是去该方法内,写我们必要的逻辑即可。
#include <jni.h>#include <string>#include <unistd.h>extern "C" {#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libavfilter/avfilter.h>#include <libavcodec/jni.h>}extern "C"JNIEXPORT jstring JNICALLJava_com_cjy_ffmepgdemo_MainActivity_ffmpegVersion(JNIEnv *env, jobject thiz) {    const char *version = av_version_info();    return env->NewStringUTF(version);}extern "C"JNIEXPORT jstring JNICALLJava_com_cjy_ffmepgdemo_MainActivity_getConfiguration(JNIEnv *env, jobject thiz) {    return env->NewStringUTF(avcodec_configuration());}运行而且输出信息:

简朴的使用流程就是这样,后续会继承记载,怎么联合这些库,来实现我们的播放功能。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 22:36, Processed in 0.186574 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表