From bdfaea5592b6941ebd803bbb3aeed02bcfb93496 Mon Sep 17 00:00:00 2001 From: xufulong Date: Thu, 7 Oct 2021 20:04:48 +0800 Subject: [PATCH] findClass when calling init method --- app/src/main/cpp/visualizer/frank_visualizer_jni.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/cpp/visualizer/frank_visualizer_jni.cpp b/app/src/main/cpp/visualizer/frank_visualizer_jni.cpp index ca90716..1b2e00e 100644 --- a/app/src/main/cpp/visualizer/frank_visualizer_jni.cpp +++ b/app/src/main/cpp/visualizer/frank_visualizer_jni.cpp @@ -17,6 +17,8 @@ struct fields_t { jfieldID context; + jclass visual_class; + jmethodID fft_method; }; static fields_t fields; @@ -41,11 +43,9 @@ FrankVisualizer *getCustomVisualizer(JNIEnv *env, jobject thiz) { } 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); 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); } @@ -53,6 +53,9 @@ VISUALIZER_FUNC(int, nativeInitVisualizer) { setCustomVisualizer(env, thiz); FrankVisualizer *mVisualizer = getCustomVisualizer(env, thiz); 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(); } @@ -76,5 +79,6 @@ VISUALIZER_FUNC(void, nativeReleaseVisualizer) { if (!mVisualizer) return; mVisualizer->release_visualizer(); delete mVisualizer; + env->DeleteGlobalRef(fields.visual_class); env->SetLongField(thiz, fields.context, 0); } \ No newline at end of file