写此文以记录android studio编译和调用ffmpeg库的操蛋过程
要调用成功最终用到android系统源代码,如果你没有系统代码,只是做应用开发,请忽略本文
下载ffmpeg以及编译就不多说了,网上很多答案,相信最终都能编译出一下这几个库,我下的是最新版本ffmpeg3.0.2,这部分代码我已经在资源中分享了,下载直接运行脚本即可编译完成
libavcodec-57.so libavdevice-57.so libavfilter-6.so libavformat-57.so libavutil-55.so libswresample-2.so libswscale-4.so
jni测试代码如下:
JNIEXPORT jint JNICALL Java_com_example_lsc_duijiangserver_MainActivity_findDecode
(JNIEnv *env, jobject obj, jint codeID) {
AVCodec*codec = NULL;
LOGE("hi %s", __func__);
/*register all formats and codecs */
av_register_all();
codec= avcodec_find_decoder(codeID);
LOGE("version=%d", (int)avcodec_version());
return 0;
}
java的调用代码则如下:
static {
// System.loadLibrary("avcodec-57");
// System.loadLibrary("avdevice-57");
// System.loadLibrary("avfilter-6");
// System.loadLibrary("avformat-57");
System.loadLibrary("avutil-55");
// System.loadLibrary("swresample-2");
// System.loadLibrary("swscale-4");
System.loadLibrary("ffmpegTest");
}
public native int findDecode(int codecID);
很简单的测试库代码,但是在studio下怎么写Android.mk来链接上述的几个库呢,我在网上找了各种办法,折腾了两天,都是最终链接时候提示找不到这几个库函数
我只能放弃了,直接在系统源代码中编译。
这样studio就不用编译jni了,先直接把jni代码删除,只留下java的loadLibrary加载so库函数
现要解决的问题就是编译出libffmpegTest.so库并adb push 到system/lib/目录下即可,app运行时会寻找该目录下的so库
编译so库:
在源代码目录新建一个文件夹,我的是jni,把上述的7个so库拷贝到该目录下,并且把前面编译ffmpeg时候生成的几个头文件目录一起拷贝到jni目录下,编写Android.mk如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libavcodec-57.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libavdevice-57.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libavfilter-6.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libavformat-57.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libavutil-55.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libswresample-2.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng optional
LOCAL_PREBUILT_LIBS := libswscale-4.so
include $(BUILD_MULTI_PREBUILT)
include $(CLEAR_VARS)
include $(CLEAR_VARS)
LOCAL_MODULE := libffmpegTest
LOCAL_SRC_FILES := com_example_lsc_duijiangserver_MainActivity.c
#LOCAL_SRC_FILES := $(call all-subdir-c-files)
LOCAL_LDLIBS := -llog -ljnigraphics -lz
LOCAL_STATIC_LIBRARIES := libcutils libc liblog
LOCAL_SHARED_LIBRARIES += libavcodec-57 libavfilter-6 libavformat-57 libavutil-55 libswresample-2 libswscale-4
LOCAL_C_INCLUDES += $(LOCAL_PATH)/ffmpeg \
$(LOCAL_PATH)/ffmpeg/libavutil \
$(LOCAL_PATH)/ffmpeg/libavcodec \
$(LOCAL_PATH)/ffmpeg/libavformat \
$(LOCAL_PATH)/ffmpeg/libavcodec \
$(LOCAL_PATH)/ffmpeg/libswscale
include $(BUILD_SHARED_LIBRARY)
最终的jni目录如下:
Android.mk ffmpeg libavcodec-57.so libavfilter libavformat-57.so libswresample libswscale-4.so
com_example_lsc_duijiangserver_MainActivity.c lib libavdevice libavfilter-6.so libavutil libswresample-2.so
com_example_lsc_duijiangserver_MainActivity.h libavcodec libavdevice-57.so libavformat libavutil-55.so libswscale
最后mm编译,adb push 到system/lib/下即可