Andriod studio3.5搭建ffmpeg helloworld(靜態(tài)庫文件)

環(huán)境

win7、jdk8、Andriod studio3.5、 NDK21


寫在前

  1. 采用編譯好的ffmpeg靜態(tài)文件(多個靜態(tài)文件,需要添加 -lz)
  2. Android studio3.5創(chuàng)建navtive c++工程已經(jīng)配置好jni demo,非常方便

步驟

  1. 編譯ffmpeg(參考 win7下ffmpeg交叉編譯android版)。得到以下文件

    image.png

  2. AS添加C++工程


    image.png
  3. 把編譯好ffmpeg文件拷貝到項目


    image.png
  4. 修改 app下 app/src/main/cpp的 CMakeList.txt
    提示:配置CMAKE_CXX_FLAGS參數(shù),-L是編譯時尋找路徑,即提示編譯器從armeabi-v7a下尋找avformat avcodec avfilter avutil swresample swscale

cmake_minimum_required(VERSION 3.4.1)

add_library(
        native-lib
        SHARED
        native-lib.cpp)

include_directories(include)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../../../libs/${CMAKE_ANDROID_ARCH_ABI}")

find_library(
        log-lib
        log)

target_link_libraries(
        native-lib
        avformat avcodec avfilter avutil swresample swscale
        andriod
        z
        ${log-lib})
  1. 修改 app的 build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.mmffndk"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'armeabi-v7a'    //修改點1
            }
        }

        ndk{                //修改點2
            abiFilters 'armeabi-v7a'
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

  1. 修改app\src\main\cpp\native-lib.cpp,測試ffmpeg集成。
    其中avutil_configuration函數(shù)是獲取ffmpeg配置信息
#include <jni.h>
#include <string>

extern "C" {
#include "include/libavutil/avutil.h"
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_mmffndk_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    return env->NewStringUTF(avutil_configuration());
}
  1. 啟動模擬。


    image.png

參考:
android全平臺編譯ffmpeg合并為單個庫實踐

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容