Skip to content

Instantly share code, notes, and snippets.

@atsushieno
Created March 8, 2022 17:52
Show Gist options
  • Save atsushieno/8e6b0bc82005cdeef65254f9e8bcec1b to your computer and use it in GitHub Desktop.
Save atsushieno/8e6b0bc82005cdeef65254f9e8bcec1b to your computer and use it in GitHub Desktop.
diff --git a/samples/hello-oboe/src/main/cpp/jni_bridge.cpp b/samples/hello-oboe/src/main/cpp/jni_bridge.cpp
index 893d641..e97c85a 100644
--- a/samples/hello-oboe/src/main/cpp/jni_bridge.cpp
+++ b/samples/hello-oboe/src/main/cpp/jni_bridge.cpp
@@ -20,6 +20,7 @@
#include "logging_macros.h"
extern "C" {
+JavaVM *vm;
/**
* Creates the audio engine
@@ -30,6 +31,9 @@ JNIEXPORT jlong JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1createEngine(
JNIEnv *env,
jclass /*unused*/) {
+ env->GetJavaVM(&vm);
+ auto cls = env->FindClass("androidx/constraintlayout/widget/ConstraintLayout");
+ assert(cls);
// We use std::nothrow so `new` returns a nullptr if the engine creation fails
HelloOboeEngine *engine = new(std::nothrow) HelloOboeEngine();
diff --git a/samples/shared/DefaultDataCallback.h b/samples/shared/DefaultDataCallback.h
index e7f858e..0b1b414 100644
--- a/samples/shared/DefaultDataCallback.h
+++ b/samples/shared/DefaultDataCallback.h
@@ -24,6 +24,9 @@
#include "IRenderableAudio.h"
#include "IRestartable.h"
+#include <jni.h>
+
+extern "C" JavaVM *vm;
/**
* This is a callback object which will render data from an `IRenderableAudio` source.
@@ -35,6 +38,11 @@ public:
virtual oboe::DataCallbackResult
onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
+ JNIEnv *env;
+ if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) == JNI_EDETACHED)
+ vm->AttachCurrentThread(&env, nullptr);
+ auto cls = env->FindClass("androidx/constraintlayout/widget/ConstraintLayout");
+ assert(cls);
if (mIsThreadAffinityEnabled && !mIsThreadAffinitySet) {
setThreadAffinity();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment