本文基于以下链接
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。