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

Finish callback implementation.

This commit is contained in:
Nolan Darilek 2020-12-30 12:19:44 -06:00
parent 0ea46b29b2
commit 8d6f40b1a5
3 changed files with 33 additions and 16 deletions

View File

@ -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(())
}

View File

@ -71,13 +71,10 @@ pub unsafe extern "C" fn Java_rs_tts_Bridge_onStart(
.unwrap();
let utterance_id = utterance_id.parse::<u64>().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::<u64>().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::<u64>().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::<u64>().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)]

View File

@ -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 {