我正在 JellyBean(Android4.2.2) 上构建我的应用演示,演示的一个模块是将 h264 流写入 mp4 文件。由于 MPEG4Writer 在源代码中完成了这项工作,所以我尝试通过 JNI 调用它。
编译源代码后,我得到了 MPEG4Writer.o 文件。在我的第一步中,我只想尝试这种方式是否可行。所以我写了 JNI 文件,这很容易,只需创建一个 MPEG4Writer 类的新实例:
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include "MPEG4Writer.h"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "MPEG4Writer", __VA_ARGS__)
jint Java_com_examole_brendontest_MainActivity_setup
(JNIEnv *env, jclass cls) {
mWriter = new MPEG4Writer("/sdcard/encode_manual.mp4");
if (mWriter != NULL)
LOGV("IT WORKS!!");
return 1;
}
和 Android.mk 文件:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := JNIMPEG4Writer
LOCAL_STATIC_LIBRARIES := MPEG4Writer
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := JNIMPEG4Writer.cpp
include $(BUILD_SHARED_LIBRARY)
然后我在Java中调用该函数:
static{
System.loadLibrary("JNIMPEG4Writer");
}
public static native int setup();
当我构建项目时它是好的,但是当我运行它时它就关闭了。日志是:
04-25 09:17:24.302: E/AndroidRuntime(31737): FATAL EXCEPTION: main
04-25 09:17:24.302: E/AndroidRuntime(31737): java.lang.ExceptionInInitializerError
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.Class.newInstanceImpl(Native Method)
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.Class.newInstance(Class.java:1319)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2122)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.ActivityThread.access$600(ActivityThread.java:148)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.os.Looper.loop(Looper.java:137)
04-25 09:17:24.302: E/AndroidRuntime(31737): at android.app.ActivityThread.main(ActivityThread.java:5102)
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 09:17:24.302: E/AndroidRuntime(31737): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 09:17:24.302: E/AndroidRuntime(31737): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 09:17:24.302: E/AndroidRuntime(31737): at dalvik.system.NativeStart.main(Native Method)
04-25 09:17:24.302: E/AndroidRuntime(31737): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load JNIMPEG4Writer from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.brendontest-2.apk,libraryPath=/data/app-lib/com.example.brendontest-2]: findLibrary returned null
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.Runtime.loadLibrary(Runtime.java:365)
04-25 09:17:24.302: E/AndroidRuntime(31737): at java.lang.System.loadLibrary(System.java:535)
04-25 09:17:24.302: E/AndroidRuntime(31737): at com.example.brendontest.MainActivity.<clinit>(MainActivity.java:12)
04-25 09:17:24.302: E/AndroidRuntime(31737): ... 15 more
我真的不知道。如果这种使用 MPEG4Writer 的方式可行吗?有人有什么主意吗?谢谢!