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 即可。

在這裏插入圖片描述
2、修改build.gradle文件
生成的工程不再講解,主要介紹需要修改的文檔。
打開app下build.gradle文件,修改defaultConfig下的externalNativeBuild配置,這裏僅增加了armeabi-v7a架構的支持,如有需要可增加其他CPU架構的支持。

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

3、複製文件到工程
將上一篇文章中生成的ffmpeg.so文件、include文件夾下頭文件複製到指定位置,複製後如下圖:
在這裏插入圖片描述
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 <jni.h>
#include <string>

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 <libavcodec/avcodec.h>

    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 <libavcodec/avcodec.h>需要用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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章