From 965bea0adf16acf5a34977822c9490015d236fdf Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 29 Dec 2020 14:10:39 -0600 Subject: [PATCH] TTS seems to initialize now. --- Cargo.toml | 3 ++- .../app/src/main/java/rs/tts/MainActivity.kt | 9 ++++++++- examples/android/src/lib.rs | 1 + src/backends/android.rs | 19 +++++++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16cfdc2..9624cd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,4 +41,5 @@ wasm-bindgen = "0.2" web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "SpeechSynthesisErrorCode", "SpeechSynthesisErrorEvent", "SpeechSynthesisEvent", "SpeechSynthesisUtterance", "Window", ] } [target.'cfg(target_os="android")'.dependencies] -jni = "0.18" \ No newline at end of file +jni = "0.18" +ndk-glue = "0.2" \ No newline at end of file diff --git a/examples/android/app/src/main/java/rs/tts/MainActivity.kt b/examples/android/app/src/main/java/rs/tts/MainActivity.kt index 2b58854..6b75440 100644 --- a/examples/android/app/src/main/java/rs/tts/MainActivity.kt +++ b/examples/android/app/src/main/java/rs/tts/MainActivity.kt @@ -1,8 +1,15 @@ package rs.tts import android.app.NativeActivity +import android.speech.tts.TextToSpeech import android.speech.tts.TextToSpeech.OnInitListener class MainActivity : NativeActivity(), OnInitListener { - override fun onInit(status:Int) {} + override fun onInit(status:Int) { + if(status == TextToSpeech.SUCCESS) { + println("Successfully initialized TTS!") + } else { + println("Failed to initialize TTS.") + } + } } \ No newline at end of file diff --git a/examples/android/src/lib.rs b/examples/android/src/lib.rs index 8a256d6..87014cc 100644 --- a/examples/android/src/lib.rs +++ b/examples/android/src/lib.rs @@ -62,5 +62,6 @@ fn run() -> Result<(), Error> { #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))] pub fn main() { + println!("xxxx In library"); run().expect("Failed to run"); } diff --git a/src/backends/android.rs b/src/backends/android.rs index 9e3f842..1caee9a 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -1,6 +1,7 @@ #[cfg(target_os = "android")] use std::sync::Mutex; +use jni::objects::JValue; use lazy_static::lazy_static; use log::info; @@ -11,25 +12,35 @@ lazy_static! { } #[derive(Clone, Debug)] -pub(crate) struct Android(BackendId); +pub(crate) struct Android { + id: BackendId, +} impl Android { pub(crate) fn new() -> Result { info!("Initializing Android backend"); let mut backend_id = NEXT_BACKEND_ID.lock().unwrap(); - let bid = BackendId::Android(*backend_id); + 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 env = vm.attach_current_thread()?; - Ok(Self(bid)) + let tts = env.new_object( + "android/speech/tts/TextToSpeech", + "(Landroid/content/Context;Landroid/speech/tts/TextToSpeech$OnInitListener;)V", + &[ + native_activity.activity().into(), + native_activity.activity().into(), + ], + )?; + Ok(Self { id }) } } impl Backend for Android { fn id(&self) -> Option { - Some(self.0) + Some(self.id) } fn supported_features(&self) -> Features {