From 8d6f40b1a5a755f64aaa99b470452f9d496dc338 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 30 Dec 2020 12:19:44 -0600 Subject: [PATCH] Finish callback implementation. --- examples/android/src/lib.rs | 5 +++-- src/backends/android.rs | 42 ++++++++++++++++++++++++++----------- src/lib.rs | 2 -- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/examples/android/src/lib.rs b/examples/android/src/lib.rs index 08a156d..7db2211 100644 --- a/examples/android/src/lib.rs +++ b/examples/android/src/lib.rs @@ -7,9 +7,9 @@ fn run() -> Result<(), Error> { .. } = tts.supported_features(); if utterance_callbacks { - /*tts.on_utterance_begin(Some(Box::new(|utterance| { + tts.on_utterance_begin(Some(Box::new(|utterance| { println!("Started speaking {:?}", utterance) - })))?;*/ + })))?; tts.on_utterance_end(Some(Box::new(|utterance| { println!("Finished speaking {:?}", utterance) })))?; @@ -57,6 +57,7 @@ fn run() -> Result<(), Error> { tts.set_volume(original_volume)?; } tts.speak("Goodbye.", false)?; + loop {} Ok(()) } diff --git a/src/backends/android.rs b/src/backends/android.rs index 16a1212..55449c3 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -71,13 +71,10 @@ pub unsafe extern "C" fn Java_rs_tts_Bridge_onStart( .unwrap(); let utterance_id = utterance_id.parse::().unwrap(); let utterance_id = UtteranceId::Android(utterance_id); - println!("Retrieving callbacks for {:?}", backend_id); let mut callbacks = CALLBACKS.lock().unwrap(); - println!("Callback keys: {:?}", callbacks.keys()); - if let Some(cb) = callbacks.get_mut(&backend_id) { - if let Some(f) = cb.utterance_begin.as_mut() { - f(utterance_id); - } + let cb = callbacks.get_mut(&backend_id).unwrap(); + if let Some(f) = cb.utterance_begin.as_mut() { + f(utterance_id); } } @@ -88,17 +85,24 @@ pub unsafe extern "C" fn Java_rs_tts_Bridge_onStop( obj: JObject, utterance_id: JString, ) { - let id = env + let backend_id = env .get_field(obj, "backendId", "I") .expect("Failed to get backend ID") .i() .expect("Failed to cast to int") as u64; + let backend_id = BackendId::Android(backend_id); let utterance_id = CString::from(CStr::from_ptr( env.get_string(utterance_id).unwrap().as_ptr(), )) .into_string() .unwrap(); - //println!("Call stop for {}", utterance_id); + let utterance_id = utterance_id.parse::().unwrap(); + let utterance_id = UtteranceId::Android(utterance_id); + let mut callbacks = CALLBACKS.lock().unwrap(); + let cb = callbacks.get_mut(&backend_id).unwrap(); + if let Some(f) = cb.utterance_end.as_mut() { + f(utterance_id); + } } #[no_mangle] @@ -108,17 +112,24 @@ pub unsafe extern "C" fn Java_rs_tts_Bridge_onDone( obj: JObject, utterance_id: JString, ) { - let id = env + let backend_id = env .get_field(obj, "backendId", "I") .expect("Failed to get backend ID") .i() .expect("Failed to cast to int") as u64; + let backend_id = BackendId::Android(backend_id); let utterance_id = CString::from(CStr::from_ptr( env.get_string(utterance_id).unwrap().as_ptr(), )) .into_string() .unwrap(); - //println!("Call done for {}", utterance_id); + let utterance_id = utterance_id.parse::().unwrap(); + let utterance_id = UtteranceId::Android(utterance_id); + let mut callbacks = CALLBACKS.lock().unwrap(); + let cb = callbacks.get_mut(&backend_id).unwrap(); + if let Some(f) = cb.utterance_stop.as_mut() { + f(utterance_id); + } } #[no_mangle] @@ -128,17 +139,24 @@ pub unsafe extern "C" fn Java_rs_tts_Bridge_onError( obj: JObject, utterance_id: JString, ) { - let id = env + let backend_id = env .get_field(obj, "backendId", "I") .expect("Failed to get backend ID") .i() .expect("Failed to cast to int") as u64; + let backend_id = BackendId::Android(backend_id); let utterance_id = CString::from(CStr::from_ptr( env.get_string(utterance_id).unwrap().as_ptr(), )) .into_string() .unwrap(); - //println!("Call error for {}", utterance_id); + let utterance_id = utterance_id.parse::().unwrap(); + let utterance_id = UtteranceId::Android(utterance_id); + let mut callbacks = CALLBACKS.lock().unwrap(); + let cb = callbacks.get_mut(&backend_id).unwrap(); + if let Some(f) = cb.utterance_end.as_mut() { + f(utterance_id); + } } #[derive(Clone)] diff --git a/src/lib.rs b/src/lib.rs index 15021d8..2e53b5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,9 +213,7 @@ impl TTS { if let Ok(backend) = backend { if let Some(id) = backend.0.id() { let mut callbacks = CALLBACKS.lock().unwrap(); - println!("Initializing callbacks for {:?}", id); callbacks.insert(id, Callbacks::default()); - println!("Keys after: {:?}", callbacks.keys()); } Ok(backend) } else {