1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 22:29:38 +00:00
tts-rs/examples/android/src/lib.rs

71 lines
2.5 KiB
Rust
Raw Normal View History

2020-12-27 16:42:41 +00:00
use tts::*;
2020-12-30 18:28:07 +00:00
// The `loop {}` below only simulates an app loop.
// Without it, the `TTS` instance gets dropped before callbacks can run.
#[allow(unreachable_code)]
2020-12-27 16:42:41 +00:00
fn run() -> Result<(), Error> {
2021-03-31 16:03:14 +00:00
let mut tts = Tts::default()?;
2020-12-27 16:42:41 +00:00
let Features {
utterance_callbacks,
..
} = tts.supported_features();
if utterance_callbacks {
2020-12-30 18:19:44 +00:00
tts.on_utterance_begin(Some(Box::new(|utterance| {
2020-12-27 16:42:41 +00:00
println!("Started speaking {:?}", utterance)
2020-12-30 18:19:44 +00:00
})))?;
2020-12-27 16:42:41 +00:00
tts.on_utterance_end(Some(Box::new(|utterance| {
println!("Finished speaking {:?}", utterance)
})))?;
tts.on_utterance_stop(Some(Box::new(|utterance| {
println!("Stopped speaking {:?}", utterance)
})))?;
}
let Features { is_speaking, .. } = tts.supported_features();
if is_speaking {
println!("Are we speaking? {}", tts.is_speaking()?);
}
tts.speak("Hello, world.", false)?;
let Features { rate, .. } = tts.supported_features();
if rate {
let original_rate = tts.get_rate()?;
tts.speak(format!("Current rate: {}", original_rate), false)?;
tts.set_rate(tts.max_rate())?;
tts.speak("This is very fast.", false)?;
tts.set_rate(tts.min_rate())?;
tts.speak("This is very slow.", false)?;
tts.set_rate(tts.normal_rate())?;
tts.speak("This is the normal rate.", false)?;
tts.set_rate(original_rate)?;
}
let Features { pitch, .. } = tts.supported_features();
if pitch {
let original_pitch = tts.get_pitch()?;
tts.set_pitch(tts.max_pitch())?;
tts.speak("This is high-pitch.", false)?;
tts.set_pitch(tts.min_pitch())?;
tts.speak("This is low pitch.", false)?;
tts.set_pitch(tts.normal_pitch())?;
tts.speak("This is normal pitch.", false)?;
tts.set_pitch(original_pitch)?;
}
let Features { volume, .. } = tts.supported_features();
if volume {
let original_volume = tts.get_volume()?;
tts.set_volume(tts.max_volume())?;
tts.speak("This is loud!", false)?;
tts.set_volume(tts.min_volume())?;
tts.speak("This is quiet.", false)?;
tts.set_volume(tts.normal_volume())?;
tts.speak("This is normal volume.", false)?;
tts.set_volume(original_volume)?;
}
tts.speak("Goodbye.", false)?;
2020-12-30 18:19:44 +00:00
loop {}
2020-12-27 16:42:41 +00:00
Ok(())
}
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
2020-12-27 16:42:41 +00:00
run().expect("Failed to run");
}