Android 引入FFmpeg库so文件

本文将介绍如何将上一篇文章编译出来的 FFmpeg so 库,引入到 Android 工程中,并验证 so 是否可以正常使用。

Android 原生对C/C++的支持

Android Studio目前推荐是用cmake编辑c/c++文件,因此本文也是使用cmake,
具体cmake的使用请参考官方文档。

创建项目

1、创建C++工程
依次点击 File -> New -> New Project,进入新建工程页面,拉到最后,选择 Native C++ 然后按照默认配置,一路 Next -> Next -> Finish 即可。

Android 引入FFmpeg库so文件_第1张图片
2、修改build.gradle文件
生成的工程不再讲解,主要介绍需要修改的文档。
打开app下build.gradle文件,修改defaultConfig下的externalNativeBuild配置,这里仅增加了armeabi-v7a架构的支持,如有需要可增加其他CPU架构的支持。

externalNativeBuild {
            cmake {
                cppFlags ""
            }
            ndk {
                abiFilters  "armeabi-v7a"
            }
        }

3、复制文件到工程
将上一篇文章中生成的ffmpeg.so文件、include文件夹下头文件复制到指定位置,复制后如下图:
Android 引入FFmpeg库so文件_第2张图片
4、修改CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.4.1)

# 支持gnu++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

# 1. 定义so库和头文件所在目录,方面后面使用
set(ffmpeg_lib_dir ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
set(ffmpeg_head_dir ${CMAKE_SOURCE_DIR})

# 2. 添加头文件目录
include_directories(include)

# 3. 添加ffmpeg相关的so库
add_library(ffmpeg
        SHARED
#        STATIC
        IMPORTED )
set_target_properties(ffmpeg
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libffmpeg.so )


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

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

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib
        ffmpeg
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

上面使用的cmake脚本,具体说明可以参考官方文档。这里为了方便定义了ffmpeg_lib_dir变量之乡so库地址。

set(ffmpeg_lib_dir ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})

5、修改native-lib.cpp文件
新增获取ffmpeg版本号ffmpegVersion()方法。

#include 
#include 

extern "C" JNIEXPORT jstring JNICALL
Java_com_liu_ffmpegtest_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
{
    #include 

    JNIEXPORT jstring JNICALL
    Java_com_liu_ffmpegtest_MainActivity_ffmpegVersion(JNIEnv *env, jobject  /* this */) {

        std::string info = av_version_info();
        return env->NewStringUTF(info.c_str());
    }
}

注意:对ffmpeg头文件的引用#include 需要用extern “C”{}包含起来,否则编译出错:

undefined reference to 'avcodec_version()'

6、修改MainActivity.java文件
增加对应的原生方法:ffmpegVersion()。

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText("版本:" + ffmpegVersion());
    }

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

参考文章

【Android 音视频开发打怪升级:FFmpeg音视频编解码篇】二、Android 引入FFmpeg

你可能感兴趣的:(ffmpeg,音视频)