美文网首页
NDK--Android Studio中配置FFmpeg动态链接

NDK--Android Studio中配置FFmpeg动态链接

作者: aruba | 来源:发表于2020-06-22 16:22 被阅读0次

    上次将FFmpeg通过linux系统编译成了arm平台下的动态链接库,生成了一个文件夹:

    其中include存放着头文件,lib存放着so库,今天将这些内容导入AS中,用于接下来音视频的开发。

    1.首先新建ndk工程,并在工程的main文件夹中新建jniLibs文件夹(AS默认的so库存放路径),并将上图中的文件夹复制到jniLibs文件夹下
    armeabi中存放so库
    include中存放头文件
    2.配置cmake,可以看之前的文章CMakeLists配置第三方so库
    设置so库路径
    set(my_lib_path ${CMAKE_SOURCE_DIR}/../jniLibs)
    
    设置include路径
    include_directories (${my_lib_path}/include)
    
    添加so库
    #将第三方库作为动态库引用
    add_library(avcodec-56
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avcodec-56
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavcodec-56.so)
    
    最后不要忘记连接到生成的so中
    target_link_libraries( 
            native-lib
            avcodec-56
            
            ${log-lib})
    
    CMakeLists完整代码:
    cmake_minimum_required(VERSION 3.4.1)
    
    #设置so库路径
    set(my_lib_path ${CMAKE_SOURCE_DIR}/../jniLibs)
    #设置include目录
    include_directories (${my_lib_path}/include)
    
    #将第三方库作为动态库引用
    add_library(avcodec-56
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avcodec-56
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavcodec-56.so)
    
    #将第三方库作为动态库引用
    add_library(avdevice-56
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avdevice-56
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavdevice-56.so)
    
    #将第三方库作为动态库引用
    add_library(avfilter-5
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avfilter-5
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavfilter-5.so)
    
    #将第三方库作为动态库引用
    add_library(avformat-56
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avformat-56
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavformat-56.so)
    
    #将第三方库作为动态库引用
    add_library(avutil-54
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(avutil-54
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libavutil-54.so)
    
    #将第三方库作为动态库引用
    add_library(postproc-53
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(postproc-53
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libpostproc-53.so)
    
    #将第三方库作为动态库引用
    add_library(swresample-1
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(swresample-1
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libswresample-1.so)
    
    #将第三方库作为动态库引用
    add_library(swscale-3
            SHARED
            IMPORTED)
    
    #指定第三方库的绝对路径
    set_target_properties(swscale-3
            PROPERTIES IMPORTED_LOCATION
            ${my_lib_path}/${ANDROID_ABI}/libswscale-3.so)
    
    
    add_library( 
            native-lib
    
            SHARED
    
            native-lib.cpp)
    
    find_library( 
            log-lib
    
            log)
    
    target_link_libraries( 
            native-lib
            avcodec-56
            avdevice-56
            avfilter-5
            avformat-56
            avutil-54
            postproc-53
            swresample-1
            swscale-3
            
            ${log-lib})
    
    配置完成后就可以在native方法中使用了:
    #include <jni.h>
    #include <string>
    
    extern "C" {
    //编码
    #include "libavcodec/avcodec.h"
    //封装格式处理
    #include "libavformat/avformat.h"
    //像素处理
    #include "libswscale/swscale.h"
    }
    
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_aruba_ffmpegapplication_MainActivity_stringFromJNI(
            JNIEnv *env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        av_register_all();
        return env->NewStringUTF(hello.c_str());
    }
    
    
    项目地址:https://gitee.com/aruba/FFmpegApplication.git

    相关文章

      网友评论

          本文标题:NDK--Android Studio中配置FFmpeg动态链接

          本文链接:https://www.haomeiwen.com/subject/cfvxfktx.html