在这里记录了一下 JNI 的动态注册,研究了好长时间,总算是写出来一份可以提供给 java 层调用的jni方法了
#include <jni.h>
#include <string>
#include <android/log.h>
#define TAG "tian.shm"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__);
jint native_tsm_player_prepare(JNIEnv * env,jobject obj,jstring source) {
LOGI("-------native_tsm_player_prepare---------source:%s",env->GetStringUTFChars(source,NULL))
return 0;
}
void native_tsm_player_start(JNIEnv * env,jobject obj) {
LOGI("-------native_tsm_player_start---------")
}
void native_tsm_player_stop(JNIEnv * env,jobject obj) {
LOGI("-------native_tsm_player_stop---------")
}
void native_tsm_player_release(JNIEnv * env,jobject obj) {
LOGI("-------native_tsm_player_release---------")
}
JNINativeMethod method []= {
{
"nativePrepare",
"(Ljava/lang/String;)I",
(jint *)native_tsm_player_prepare
},
{
"nativeRelease",
"()V",
(void *)native_tsm_player_release
},
{
"nativeStop",
"()V",
(void *)native_tsm_player_stop
},
{
"nativeStart",
"()V",
(void *)native_tsm_player_start
}
};
int JNI_OnLoad(JavaVM
*vm,
void *unused
) {
JNIEnv *env;
int env_result = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
if (env_result != JNI_OK) {
return JNI_FALSE;
}
jclass cls = env->FindClass("com/example/tsmplayer/TsmPlayer");
env->RegisterNatives(cls, method, sizeof(method) / sizeof(JNINativeMethod));
return JNI_VERSION_1_6;
}