在android 移植 ffmpeg (一)中已经对环境进行了设置。 这一章将重点讨论怎么在应用中加入ffmpeg组件。
所有测试都将在 Android Studio工具中进行。
测试例子源地址: https://github.com/roman10/android-ffmpeg-tutorial
本例子是在android-ffmpeg-tutorial01 基础上进行了简单调整。调整后的源码页:http://download.csdn.net/detail/net_wolf_007/9620856
MainActivity.java 主Java程序, 后面会对程序进行说明
Utils.java 工具类。提供了文件拷贝功能,程序中主要用于把1.mp4导入到指定目录
./libs/armeabi/
目录下
需要通过命令行来生成
命令行运行到main目录下,调用ndk_build.
172-15-70-196:main jerome$ pwd
/Users/jerome/dev/ffmpeg/android-ffmpeg-tutorial01/app/src/main
172-15-70-196:main jerome$ /Users/jerome/dev/android-ndk-r12b/ndk-build
[armeabi] Compile thumb : tutorial01 <= tutorial01.c
jni/tutorial01.c: In function 'naMain':
jni/tutorial01.c:117:2: warning: 'codec' is deprecated (declared at /Users/jerome/dev/android-ndk-r12b/sources/ffmpeg/android/include/libavformat/avformat.h:881) [-Wdeprecated-declarations]
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
^
[armeabi] SharedLibrary : libtutorial01.so
[armeabi] Install : libtutorial01.so => libs/armeabi/libtutorial01.so
[armeabi] Install : libavformat-57.so => libs/armeabi/libavformat-57.so
[armeabi] Install : libavcodec-57.so => libs/armeabi/libavcodec-57.so
[armeabi] Install : libswscale-4.so => libs/armeabi/libswscale-4.so
[armeabi] Install : libswresample-2.so => libs/armeabi/libswresample-2.so
[armeabi] Install : libavutil-55.so => libs/armeabi/libavutil-55.so
172-15-70-196:main jerome$
//#if !HAVE_LOG2
//#undef log2
#define log2(x) (log(x) * 1.44269504088896340736)
//#endif /* HAVE_LOG2 */
//#if !HAVE_LOG2F
//#undef log2f
#define log2f(x) ((float)log2(x))
//#endif /* HAVE_LOG2F */
ifndef NDK_PROJECT_PATH
ifneq (,$(strip $(wildcard AndroidManifest.xml)))
NDK_PROJECT_PATH := .
else
ifneq (,$(strip $(wildcard jni/Android.mk)))
NDK_PROJECT_PATH := .
endif
endif
endif
ifndef NDK_PROJECT_PATH
NDK_PROJECT_PATH := $(call find-project-dir,.,jni/Android.mk)
endif
ifndef NDK_PROJECT_PATH
NDK_PROJECT_PATH := $(call find-project-dir,.,AndroidManifest.xml)
endif
_build_script := $(strip $(wildcard $(APP_PROJECT_PATH)/jni/Android.mk))
ifndef _build_script
$(call __ndk_info,There is no Android.mk under $(APP_PROJECT_PATH)/jni)
$(call __ndk_info,If this is intentional, please define APP_BUILD_SCRIPT to point)
$(call __ndk_info,to a valid NDK build script.)
$(call __ndk_error,Aborting...)
endif
APP_BUILD_SCRIPT := $(_build_script)
说明: 如果没找到
APP_BUILD_SCRIPT的定义,就查找APP_PROJECT_PATH下的 jni/Android.mk, 如果找到就赋值给APP_BUILD_SCRIPT。如果没有找到,就报错。Android NDK: Your APP_BUILD_SCRIPT points to an unknown file: ./jni/Android.mk
APP_PROJECT_PATH := $(strip $(APP_PROJECT_PATH))
ifndef APP_PROJECT_PATH
APP_PROJECT_PATH := $(NDK_PROJECT_PATH)
endif
如果定义了环境变量
APP
_PROJECT_PATH, 就使用定义的值,如果没有就使用 NDK_PROJECT_PATH的值。
$(call import-module,ffmpeg/android)查找逻辑是通过查找环境变量:NDK_MODULE_PATH下的对应模块的Android.mk文件, 上例中就是查找 ffmpeg/android/Android.mk文件。如果找不到就报错。Android NDK: jni/Android.mk: Cannot find module with tag 'ffmpeg/android' in import path
NDK_MODULE_PATH := $(strip $(NDK_MODULE_PATH))
ifdef NDK_MODULE_PATH
ifneq ($(words $(NDK_MODULE_PATH)),1)
$(call __ndk_info,ERROR: You NDK_MODULE_PATH variable contains spaces)
$(call __ndk_info,Please fix the error and start again.)
$(call __ndk_error,Aborting)
endif
endif
$(call import-init)
$(foreach __path,$(subst $(HOST_DIRSEP),$(space),$(NDK_MODULE_PATH)),\
$(call import-add-path,$(__path))\
)
$(call import-add-path-optional,$(NDK_ROOT)/sources)
$(call import-add-path-optional,$(NDK_ROOT)/../development/ndk/sources)