Android-經過cmake集成ffmpeg

今天給你們介紹把上次編譯出來的ffmpeg文件集成到Android中。本文使用的cmake構建的ndk環境。java

準備ffmpeg庫文件

經過上篇文章Android-ffmpeg編譯so文件,相信你們已經編譯出來ffmpeg的頭文件和so文件了。android

若是你們沒編譯出來也不影響,你們能夠 點擊下載ffmpeg庫文件

cmake構建當前項目的NKD環境,同時集成ffmpeg

把咱們當前的項目經過cmake構建一個NDK環境出來bash

  1. 在當前項目moudle下建立一個CMakeLists.txt文件
  2. 在當前moudle的build文件中添加如下內容
android{
    ...
    defaultConfig{
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
            ndk {
                abiFilters "armeabi" #我當前編譯的是arm版本,因此此處指填寫arm
            }
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}
複製代碼
  1. 把ffmpeg庫文件導入項目,同時建立本身的C++源文件native-lib.cpp,如圖所示

4. 編輯 CMakeLists.txt 文件

# 設置最小使用版本
cmake_minimum_required(VERSION 3.4.1)

# 添加本地so庫 native-lib:這個是聲明引用so庫的名稱 SHARED:表示共享so庫文件
# 構建so庫的源文件
add_library(
    native-lib
    SHARED
    src/main/cpp/native-lib.cpp
)
# 使用系統ndk 提供的庫,如 log庫
# log-lib 這個指定的是在NDK庫中每一個類型的庫會存放一個特定的位置,而log庫存放
# 在log-lib中
# log 指定使用log庫
find_library(
    log-lib
    log
)
#----------------------ffmpeg的庫文件------------
# 加載頭文件
include_directories(src/main/cpp/include)

# 加載avcodec-57庫
add_library( avcodec-57
             SHARED
             IMPORTED)
set_target_properties( avcodec-57
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavcodec-57.so)

add_library( avdevice-57
             SHARED
             IMPORTED)
set_target_properties( avdevice-57
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavdevice-57.so)
add_library( avfilter-6
                SHARED
                IMPORTED)
set_target_properties( avfilter-6
                          PROPERTIES IMPORTED_LOCATION
                          ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavfilter-6.so)
add_library( avformat-57
             SHARED
             IMPORTED)
set_target_properties( avformat-57
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavformat-57.so)
add_library( avutil-55
             SHARED
             IMPORTED)
set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libavutil-55.so)
add_library( swresample-2
             SHARED
             IMPORTED)
set_target_properties( swresample-2
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libswresample-2.so)
add_library( swscale-4
             SHARED
             IMPORTED)
set_target_properties( swscale-4
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libswscale-4.so)
#----------------------end-----------------------

# 若是你本地的庫(native-lib)想要調用log庫的方法,
# 那麼就須要配置這個屬性,意思是把NDK庫關聯到本地庫。
# 第一個參數表示本地的庫 native-lib 要調用到log庫的方法,即要被關聯的庫名稱,log-lib 要關聯的庫名稱
target_link_libraries(
    native-lib
    #ffmpeg------start----------
    avcodec-57
    avdevice-57
    avfilter-6
    avformat-57
    avutil-55
    swresample-2
    swscale-4
    #ffmpeg------end------------
    ${log-lib}
)
複製代碼
  1. 編譯一下項目,這個時候就集成成功了

測試一下

  1. Java層,建立一個native方法,同時加載native-lib
public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text = (TextView) findViewById(R.id.text);
        text.setText(helloNDK("你好 C++ \n"));
    }
    public native String helloNDK(String msg);
}
複製代碼
  1. C++層,對應建立helloNDK的方法
#include <jni.h>
#include <android/log.h>
extern "C"{
#include <libavformat/avformat.h>
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ffmpeg_1test_MainActivity_helloNDK(JNIEnv* env,jobject,jstring msg){
    char* chello = (char *) env->GetStringUTFChars(msg, JNI_FALSE);
    __android_log_print(ANDROID_LOG_ERROR,"tag","c : %s",chello);#log日誌打印
    __android_log_print(ANDROID_LOG_ERROR,"tag","編碼器配置: %s",avcodec_configuration());#log日誌打印
    char * newstr = strcat(chello,avcodec_configuration());#將java傳過來的字符串和編碼器配置信息拼接起來並返回去
    return env->NewStringUTF(newstr);
}
複製代碼
  1. 運行獲得如下結果

相關文章
相關標籤/搜索