Return utterance IDs when speech succeeds.

This commit is contained in:
Nolan Darilek 2020-12-30 09:44:47 -06:00
parent 5634fdb393
commit 22ee9863d6
2 changed files with 19 additions and 3 deletions

View File

@ -17,6 +17,7 @@ lazy_static! {
static ref BRIDGE: Mutex<Option<GlobalRef>> = Mutex::new(None); static ref BRIDGE: Mutex<Option<GlobalRef>> = Mutex::new(None);
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0); static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
static ref PENDING_INITIALIZATIONS: RwLock<HashSet<u64>> = RwLock::new(HashSet::new()); static ref PENDING_INITIALIZATIONS: RwLock<HashSet<u64>> = RwLock::new(HashSet::new());
static ref NEXT_UTTERANCE_ID: Mutex<u64> = Mutex::new(0);
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -121,7 +122,13 @@ impl Backend for Android {
let tts = self.tts.as_obj(); let tts = self.tts.as_obj();
let text = env.new_string(text)?; let text = env.new_string(text)?;
let queue_mode = if interrupt { 0 } else { 1 }; 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, tts,
"speak", "speak",
"(Ljava/lang/CharSequence;ILandroid/os/Bundle;Ljava/lang/String;)I", "(Ljava/lang/CharSequence;ILandroid/os/Bundle;Ljava/lang/String;)I",
@ -129,10 +136,15 @@ impl Backend for Android {
text.into(), text.into(),
queue_mode.into(), queue_mode.into(),
JObject::null().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> { fn stop(&mut self) -> Result<(), Error> {

View File

@ -71,6 +71,8 @@ pub enum UtteranceId {
WinRT(u64), WinRT(u64),
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
AvFoundation(id), AvFoundation(id),
#[cfg(target_os = "android")]
Android(u64),
} }
unsafe impl Send for UtteranceId {} unsafe impl Send for UtteranceId {}
@ -105,6 +107,8 @@ pub enum Error {
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
#[error("Value not received")] #[error("Value not received")]
NoneError, NoneError,
#[error("Operation failed")]
OperationFailed,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[error("JavaScript error: [0])]")] #[error("JavaScript error: [0])]")]
JavaScriptError(wasm_bindgen::JsValue), JavaScriptError(wasm_bindgen::JsValue),