Android之集成FFmpeg到APP项目

// 国内源 ...
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
// 环境 ...
Android Studio 3.6.1
SDK Platforms > Android 10.0 (Q) API Level 29 Revision 4
SDK Tools >
    Android SDK Build-Tools 30-rc1
    NDK (Side by side)
    CMake
    Android Emulator
    Android SDK Platform-Tools
// 准备工作:Android之NDK编译FFmpeg教程 
// 编译生成以下文件 ...
ffmpeg-4.2.2/android/armv7-a
ffmpeg-4.2.2/android/armv8-a
1. 新建一个Android Native C++工程 ...
2. 拷贝库文件到项目 ...
2-1. 拷贝 armv7-a/lib 内 .so 文件到 MyApplication\app\src\main\cpp\libs\armeabi-v7a
2-2. 拷贝 armv8-a/lib 内 .so 文件到 MyApplication\app\src\main\cpp\libs\arm64-v8a
3. 新建 MyApplication/appsrc/main/cpp 目录
3-1. 拷贝 armv7-a/include 文件夹到 MyApplication/appsrc/main/cpp 
3-2. 在 MyApplication\app\src\main\cpp 中新建 native-lib.cpp 内容如下 ...

    #include <jni.h>
    #include <string>
    extern "C" {
    #include <libavcodec/avcodec.h> //引入ffmpeg的包
    }
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_goodboy_myplay_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(av_version_info());//返回ffmpeg的版本
    }
4. 在 MyApplication\app 目录中新建 CMakeLists.txt 内容如下 ...

    cmake_minimum_required(VERSION 3.4.1)

    add_library( # Sets the name of the library.
        native-lib
        SHARED
        src/main/cpp/native-lib.cpp)
    include_directories(src/main/cpp/include)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}")

    find_library( # Sets the name of the path variable.
        log-lib
        log)


    target_link_libraries( # Specifies the target library.
        native-lib
        avcodec avfilter avformat avutil swresample swscale
        ${log-lib})
5. 修改 MyApplication\app\build.gradle 文件部分内容 ...


    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"

        defaultConfig {
        applicationId "com.danoo.xplay"
        minSdkVersion 21 
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
            cppFlags ""
            abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }
        }

        sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/cpp/libs/']
        }
        }

        buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        }

        externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
            version "3.10.2"
        }
        }
        ndkVersion = '20.0.5594570'

    }

    ...
6. 修改 MainActivity 文件 ...

    public class MainActivity extends AppCompatActivity {

        static {
        System.loadLibrary("native-lib");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
            Log.d(">>>", stringFromJNI());
            }
        });
        }

        public native String stringFromJNI();
    }