From 32f57d85785052063170dcb30d279a8b8555613c Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 29 Dec 2020 16:24:08 -0600 Subject: [PATCH] Speak calls pass to Java, but don't work since something isn't bound to the engine. --- src/backends/android.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/backends/android.rs b/src/backends/android.rs index 518b25e..65fa756 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -1,7 +1,9 @@ #[cfg(target_os = "android")] use std::sync::Mutex; -use jni::objects::GlobalRef; +use jni::objects::{GlobalRef, JObject}; +use jni::JNIEnv; +use jni::JavaVM; use lazy_static::lazy_static; use log::info; @@ -24,8 +26,7 @@ impl Android { let id = BackendId::Android(*backend_id); *backend_id += 1; let native_activity = ndk_glue::native_activity(); - let vm_ptr = native_activity.vm(); - let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }?; + let vm = Self::vm()?; let env = vm.attach_current_thread()?; let tts = env.new_object( "android/speech/tts/TextToSpeech", @@ -35,9 +36,17 @@ impl Android { native_activity.activity().into(), ], )?; + println!("Creating global ref"); let tts = env.new_global_ref(tts)?; + println!("Returning"); Ok(Self { id, tts }) } + + fn vm() -> Result { + let native_activity = ndk_glue::native_activity(); + let vm_ptr = native_activity.vm(); + unsafe { jni::JavaVM::from_raw(vm_ptr) } + } } impl Backend for Android { @@ -58,6 +67,26 @@ impl Backend for Android { fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error> { println!("Speaking {}, {:?}", text, interrupt); + let vm = Self::vm()?; + println!("Retrieved"); + let env = vm.attach_current_thread()?; + println!("attached"); + let tts = self.tts.as_obj(); + let text = env.new_string(text)?; + let queue_mode = if interrupt { 0 } else { 1 }; + println!("Calling"); + env.call_method( + tts, + "speak", + "(Ljava/lang/CharSequence;ILandroid/os/Bundle;Ljava/lang/String;)I", + &[ + text.into(), + queue_mode.into(), + JObject::null().into(), + JObject::null().into(), + ], + )?; + println!("Returning"); Ok(None) }