本例子簡(jiǎn)單的輸出FFmpeg其配置信息等
怎么編譯FFmpeg的so庫(kù),可以參考我之前的編譯過(guò)程http://www.reibang.com/p/228f38e9aa9c
項(xiàng)目的目錄:
把之前編譯好ffmpemg的頭文件放到include里疼邀,so文件按平臺(tái)文件夾放好喂江。這里只編譯了armeabi平臺(tái)的。jniLibs文件夾沒(méi)有的話自己建一個(gè)旁振,也可以在build.gradle里指定一個(gè)目錄获询。這里jniLibs的路徑是AS的默認(rèn)配置,無(wú)需修改build.gradle
Activity代碼:
public class FFmpegHelloActivity extends AppCompatActivity {
static {
System.loadLibrary("ffmpeg-hello");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ffmpeg_hello);
TextView tv = (TextView) findViewById(R.id.tv_info);
tv.setText(avcodeConfigFromJni());
}
public native String avcodeConfigFromJni();
}
cpp代碼:
頭文件ffmpeg.h
#include <jni.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_clam314_hellojni_FFmpegHelloActivity_avcodeConfigFromJni(JNIEnv *env, jobject instance);
源代碼ffmpeg-hello.cpp:
#include <stdio.h>
#include "ffmpeg-hello.h"
extern "C"
{
#include <libavcodec/avcodec.h>
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_clam314_hellojni_FFmpegHelloActivity_avcodeConfigFromJni(JNIEnv *env, jobject instance) {
char info[10000] = { 0 };
sprintf(info, "%s\n", avcodec_configuration());
return env->NewStringUTF(info);
}
CmakeLists.txt
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
#設(shè)置一個(gè)路徑拐袜。
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)
add_library(
avcodec-57
SHARED
IMPORTED)
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libavcodec-57.so )
add_library(
avfilter-6
SHARED
IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libavfilter-6.so )
add_library(
avformat-57
SHARED
IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libavformat-57.so )
add_library(
avutil-55
SHARED
IMPORTED)
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libavutil-55.so )
add_library(
swresample-2
SHARED
IMPORTED)
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libswresample-2.so )
add_library(
swscale-4
SHARED
IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libswscale-4.so )
add_library( avdevice-57
SHARED
IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libavdevice-57.so )
#支持-std=gnu++11
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#判斷編譯器類型,如果是gcc編譯器,則在編譯選項(xiàng)中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
add_library( ffmpeg-hello
SHARED
src/main/cpp/ffmpeg-hello.cpp )
include_directories(src/main/jniLibs/include/)
target_link_libraries( native-lib avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
${log-lib} )
target_link_libraries( ffmpeg-hello avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
${log-lib} )
CMAKE_SOURCE_DIR 是CmakeLists.txt所在目錄吉嚣,Cmake的默認(rèn)值
將 distribution_DIR指定為jniLibs的路徑,后面用于統(tǒng)一指定so庫(kù)的路徑
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)
#添加一個(gè)so庫(kù)蹬铺,每一個(gè)都要添加尝哆,無(wú)論是自定義還是預(yù)編譯的
add_library( avdevice-57#so文件的名字
SHARED #類型是共享庫(kù)
IMPORTED)#預(yù)編譯的so指定為IMPORTED,自定義的eg:ffmpeg-hello的就填路徑
#每加入一個(gè)預(yù)定義的so庫(kù)都需要執(zhí)行這命令
set_target_properties( avdevice-57#so文件的名字
PROPERTIES IMPORTED_LOCATION
#so庫(kù)所在路徑甜攀,ANDROID_ABI是平臺(tái)的名字秋泄,Cmake的默認(rèn)值
${distribution_DIR}/${ANDROID_ABI}/libavdevice-57.so )
指定預(yù)編譯庫(kù)的頭文件路徑,另外自定義的頭文件放cpp文件夾里就行
include_directories(src/main/jniLibs/include/)
鏈接各個(gè)so庫(kù)规阀,每一個(gè)自定義的so,都需要和其用到so鏈接一遍恒序。eg:這里的ffmpe-hello就用到了ffmpeg的so庫(kù),但沒(méi)有用到native-lib谁撼,native-lib就無(wú)須在參數(shù)里面歧胁。若沒(méi)有調(diào)用此命令,調(diào)用其他so庫(kù)的方法的地方會(huì)報(bào) error: undefined reference to
target_link_libraries( ffmpeg-hello avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
${log-lib} )
結(jié)果: