ffmpeg系列之移植到安卓中调用

基础调用

1)编译安卓需要的so,请参考

编译ffmpeg的安卓版本

2)将so引入安卓中使用

在module的src/main/下创建cpp和jniLibs模块,jniLibs中存放so文件
在这里插入图片描述

3)cpp中进行cmake配置

将ffmpeg的头文件拷贝到src/main/cpp下
在这里插入图片描述
配置cmakelist

# 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.10.2)

# Declares and names the project.

project("ffmpeg")

#设置FFmpeg头文件的路径,共享库,include里面h文件调用其他h文件用到的
include_directories(
        include#因为和CMakeLists.txt在同一级,所以直接写include
)

add_library(ffmpeg
        SHARED
        ffmpeg_util.cpp)

find_library(
        log-lib
        log)

add_library(avcodec
        SHARED
        IMPORTED)
#给avcodec这个变量赋值
set_target_properties(avcodec
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavcodec.so)


add_library(avdevice
        SHARED
        IMPORTED)
set_target_properties(avdevice
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavdevice.so)

add_library(avformat
        SHARED
        IMPORTED)
set_target_properties(avformat
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavformat.so)

add_library(avutil
        SHARED
        IMPORTED)
set_target_properties(avutil
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavutil.so)


add_library(swresample
        SHARED
        IMPORTED)
set_target_properties(swresample
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libswresample.so)

add_library(swscale
        SHARED
        IMPORTED)
set_target_properties(swscale
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libswscale.so)

add_library(avfilter
        SHARED
        IMPORTED)
set_target_properties(avfilter
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libavfilter.so)



target_link_libraries(ffmpeg
        ${log-lib}
        avcodec swresample avutil)

4) 编写jni测试代码

#include <android/log.h>

#include <jni.h>
#include <stdlib.h>


#ifdef __cplusplus
extern "C"
{
#endif
/*Include ffmpeg header file*/
#include "libavcodec/avcodec.h"

#ifdef __cplusplus
}
#endif

extern "C" JNIEXPORT jstring JNICALL
Java_com_cd_lib_ffmpeg_FfmpegUtil_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    return env->NewStringUTF(avcodec_configuration());
}

根据jni规则编码java

public class FfmpegUtil {

    // Used to load the 'ffmpeg' library on application startup.
    static {
        System.loadLibrary("ffmpeg");
    }

    /**
     * A native method that is implemented by the 'ffmpeg' native library,
     * which is packaged with this application.
     */
    public static native String stringFromJNI();
}

5) 配置gradle相关位置



android {
    ...

    defaultConfig {
       ...
        ndk {
            abiFilters  'armeabi-v7a', 'arm64-v8a'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
    ...
}

6)build运行即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值