1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 12:39:36 +00:00

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 NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
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)]
@ -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> {

View File

@ -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),