From 22ee9863d61894f6f01e39f92e5671da2b6a01ee Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 30 Dec 2020 09:44:47 -0600 Subject: [PATCH] Return utterance IDs when speech succeeds. --- src/backends/android.rs | 18 +++++++++++++++--- src/lib.rs | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/backends/android.rs b/src/backends/android.rs index a70b054..577a3a4 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -17,6 +17,7 @@ lazy_static! { static ref BRIDGE: Mutex> = Mutex::new(None); static ref NEXT_BACKEND_ID: Mutex = Mutex::new(0); static ref PENDING_INITIALIZATIONS: RwLock> = RwLock::new(HashSet::new()); + static ref NEXT_UTTERANCE_ID: Mutex = Mutex::new(0); } #[allow(non_snake_case)] @@ -121,7 +122,13 @@ impl Backend for Android { let tts = self.tts.as_obj(); let text = env.new_string(text)?; let queue_mode = if interrupt { 0 } else { 1 }; - env.call_method( + let mut utterance_id = NEXT_UTTERANCE_ID.lock().unwrap(); + let uid = *utterance_id; + *utterance_id += 1; + drop(utterance_id); + let id = UtteranceId::Android(uid); + let uid = env.new_string(uid.to_string())?; + let rv = env.call_method( tts, "speak", "(Ljava/lang/CharSequence;ILandroid/os/Bundle;Ljava/lang/String;)I", @@ -129,10 +136,15 @@ impl Backend for Android { text.into(), queue_mode.into(), JObject::null().into(), - JObject::null().into(), + uid.into(), ], )?; - Ok(None) + let rv = rv.i()? as i32; + if rv == 0 { + Ok(Some(id)) + } else { + Err(Error::OperationFailed) + } } fn stop(&mut self) -> Result<(), Error> { diff --git a/src/lib.rs b/src/lib.rs index ff0a46d..2e53b5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,8 @@ pub enum UtteranceId { WinRT(u64), #[cfg(any(target_os = "macos", target_os = "ios"))] AvFoundation(id), + #[cfg(target_os = "android")] + Android(u64), } unsafe impl Send for UtteranceId {} @@ -105,6 +107,8 @@ pub enum Error { IO(#[from] std::io::Error), #[error("Value not received")] NoneError, + #[error("Operation failed")] + OperationFailed, #[cfg(target_arch = "wasm32")] #[error("JavaScript error: [0])]")] JavaScriptError(wasm_bindgen::JsValue),