findClass when calling init method

dev
xufulong 3 years ago
parent 31c09ad85b
commit bdfaea5592
  1. 10
      app/src/main/cpp/visualizer/frank_visualizer_jni.cpp

@ -17,6 +17,8 @@
struct fields_t { struct fields_t {
jfieldID context; jfieldID context;
jclass visual_class;
jmethodID fft_method;
}; };
static fields_t fields; static fields_t fields;
@ -41,11 +43,9 @@ FrankVisualizer *getCustomVisualizer(JNIEnv *env, jobject thiz) {
} }
void fft_callback(JNIEnv *jniEnv, int8_t * arg, int samples) { void fft_callback(JNIEnv *jniEnv, int8_t * arg, int samples) {
jclass visual_class = jniEnv->FindClass(className);
jmethodID fft_method = jniEnv->GetStaticMethodID(visual_class, "onFftCallback", "([B)V");
jbyteArray dataArray = jniEnv->NewByteArray(samples); jbyteArray dataArray = jniEnv->NewByteArray(samples);
jniEnv->SetByteArrayRegion(dataArray, 0, samples, arg); jniEnv->SetByteArrayRegion(dataArray, 0, samples, arg);
jniEnv->CallStaticVoidMethod(visual_class, fft_method, dataArray); jniEnv->CallStaticVoidMethod(fields.visual_class, fields.fft_method, dataArray);
jniEnv->DeleteLocalRef(dataArray); jniEnv->DeleteLocalRef(dataArray);
} }
@ -53,6 +53,9 @@ VISUALIZER_FUNC(int, nativeInitVisualizer) {
setCustomVisualizer(env, thiz); setCustomVisualizer(env, thiz);
FrankVisualizer *mVisualizer = getCustomVisualizer(env, thiz); FrankVisualizer *mVisualizer = getCustomVisualizer(env, thiz);
if (!mVisualizer) return -2; if (!mVisualizer) return -2;
jclass mVisualClass = env->FindClass(className);
fields.visual_class = (jclass) env->NewGlobalRef(mVisualClass);
fields.fft_method = env->GetStaticMethodID(fields.visual_class, "onFftCallback", "([B)V");
return mVisualizer->init_visualizer(); return mVisualizer->init_visualizer();
} }
@ -76,5 +79,6 @@ VISUALIZER_FUNC(void, nativeReleaseVisualizer) {
if (!mVisualizer) return; if (!mVisualizer) return;
mVisualizer->release_visualizer(); mVisualizer->release_visualizer();
delete mVisualizer; delete mVisualizer;
env->DeleteGlobalRef(fields.visual_class);
env->SetLongField(thiz, fields.context, 0); env->SetLongField(thiz, fields.context, 0);
} }
Loading…
Cancel
Save