本文基於以下鏈接
1.新建android項目,勾選include C++ support
2.勾選Exceptions Support (-fexceptions)、Runtime Type Information Support (-frtti)
3.Finish後的項目目錄
4.libs下創建armeabi目錄,拷貝ffmpeg編譯後的so文件到armeabi下,拷貝include文件到libs目錄下
5.app下的build.gradle修改如下:
6.CMakeLists.txt修改如下:
cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp
)
include_directories(libs/include)
set(DIR ../../../../libs)
MESSAGE("打印")
MESSAGE("路徑 " ${DIR})
add_library(avcodec-56
SHARED
IMPORTED)
set_target_properties(avcodec-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavcodec-56.so)
add_library(avdevice-56
SHARED
IMPORTED)
set_target_properties(avdevice-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavdevice-56.so)
add_library(avformat-56
SHARED
IMPORTED)
set_target_properties(avformat-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavformat-56.so)
add_library(avutil-54
SHARED
IMPORTED)
set_target_properties(avutil-54
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavutil-54.so)
add_library(swresample-1
SHARED
IMPORTED)
set_target_properties(swresample-1
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswresample-1.so)
add_library(swscale-3
SHARED
IMPORTED)
set_target_properties(swscale-3
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswscale-3.so)
add_library(avfilter-5
SHARED
IMPORTED)
set_target_properties(avfilter-5
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavfilter-5.so)
find_library( log-lib
log )
target_link_libraries( native-lib
avcodec-56
avdevice-56
avformat-56
avutil-54
swresample-1
swscale-3
-landroid # Add this.
${log-lib} )
7.cpp目錄下新建native-lib.h頭文件,native-lib.cpp是默認生成的
native-lib.h
//
// Created by ygdx_lk on 17/11/1.
//
#ifndef FFMPEG_NATIVE_LIB_H
#define FFMPEG_NATIVE_LIB_H
#include <jni.h>
#include <string>
#include <android/log.h>
extern "C" {
//編碼
#include "libavcodec/avcodec.h"
//封裝格式處理
#include "libavformat/avformat.h"
//像素處理
#include "libswscale/swscale.h"
#include <android/native_window_jni.h>
#include <unistd.h>
JNIEXPORT jstring JNICALL Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */);
}
#define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"jason",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"jason",FORMAT,##__VA_ARGS__);
#endif //FFMPEG_NATIVE_LIB_H
native-lib.cpp
#include "native-lib.h"
jstring Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}
8.MainActivity中配置如下:
9.運行程序,如果可以正常運行,說明ffmpeg的配置已經ok。