From 16a6f6378ad2d08958027db48bea026f3e77de12 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 6 Jul 2020 12:52:18 -0500 Subject: [PATCH] Under WinRT, recreate player completely when interruption is requested. --- src/backends/speech_dispatcher.rs | 4 ++-- src/backends/tolk.rs | 4 ++-- src/backends/web.rs | 4 ++-- src/backends/winrt.rs | 19 +++++++++++++------ src/lib.rs | 8 ++++---- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/backends/speech_dispatcher.rs b/src/backends/speech_dispatcher.rs index 0d7252c..6a36d6c 100644 --- a/src/backends/speech_dispatcher.rs +++ b/src/backends/speech_dispatcher.rs @@ -25,7 +25,7 @@ impl Backend for SpeechDispatcher { } } - fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); if interrupt { self.stop()?; @@ -41,7 +41,7 @@ impl Backend for SpeechDispatcher { Ok(()) } - fn stop(&self) -> Result<(), Error> { + fn stop(&mut self) -> Result<(), Error> { trace!("stop()"); self.0.cancel(); Ok(()) diff --git a/src/backends/tolk.rs b/src/backends/tolk.rs index ae877fc..176fdb7 100644 --- a/src/backends/tolk.rs +++ b/src/backends/tolk.rs @@ -29,13 +29,13 @@ impl Backend for Tolk { } } - fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); self.0.speak(text, interrupt); Ok(()) } - fn stop(&self) -> Result<(), Error> { + fn stop(&mut self) -> Result<(), Error> { trace!("stop()"); self.0.silence(); Ok(()) diff --git a/src/backends/web.rs b/src/backends/web.rs index 7367f80..664793c 100644 --- a/src/backends/web.rs +++ b/src/backends/web.rs @@ -32,7 +32,7 @@ impl Backend for Web { } } - fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap(); utterance.set_rate(self.rate); @@ -48,7 +48,7 @@ impl Backend for Web { Ok(()) } - fn stop(&self) -> Result<(), Error> { + fn stop(&mut self) -> Result<(), Error> { trace!("stop()"); if let Some(window) = web_sys::window() { let speech_synthesis = window.speech_synthesis().unwrap(); diff --git a/src/backends/winrt.rs b/src/backends/winrt.rs index 8e502ae..1e10d91 100644 --- a/src/backends/winrt.rs +++ b/src/backends/winrt.rs @@ -24,9 +24,9 @@ pub struct WinRT { impl WinRT { pub fn new() -> std::result::Result { info!("Initializing WinRT backend"); + let playback_list = MediaPlaybackList::new()?; let player = MediaPlayer::new()?; player.set_auto_play(true)?; - let playback_list = MediaPlaybackList::new()?; player.set_source(&playback_list)?; Ok(Self { synth: SpeechSynthesizer::new()?, @@ -34,6 +34,14 @@ impl WinRT { playback_list: playback_list, }) } + + fn reinit_player(&mut self) -> std::result::Result<(), Error> { + self.playback_list = MediaPlaybackList::new()?; + self.player = MediaPlayer::new()?; + self.player.set_auto_play(true)?; + self.player.set_source(&self.playback_list)?; + Ok(()) + } } impl Backend for WinRT { @@ -47,7 +55,7 @@ impl Backend for WinRT { } } - fn speak(&self, text: &str, interrupt: bool) -> std::result::Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> std::result::Result<(), Error> { trace!("speak({}, {})", text, interrupt); if interrupt { self.stop()?; @@ -61,7 +69,7 @@ impl Backend for WinRT { let index = self.playback_list.current_item_index()?; let total = self.playback_list.items()?.size()?; if total != 0 && index == total - 1 { - self.playback_list.items()?.clear()?; + self.reinit_player()?; } } self.playback_list.items()?.append(item)?; @@ -71,10 +79,9 @@ impl Backend for WinRT { Ok(()) } - fn stop(&self) -> std::result::Result<(), Error> { + fn stop(&mut self) -> std::result::Result<(), Error> { trace!("stop()"); - self.player.pause()?; - self.playback_list.items()?.clear()?; + self.reinit_player()?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 587b20f..2fa5590 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,8 +51,8 @@ pub enum Error { pub trait Backend { fn supported_features(&self) -> Features; - fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>; - fn stop(&self) -> Result<(), Error>; + fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error>; + fn stop(&mut self) -> Result<(), Error>; fn min_rate(&self) -> f32; fn max_rate(&self) -> f32; fn normal_rate(&self) -> f32; @@ -131,7 +131,7 @@ impl TTS { /** * Speaks the specified text, optionally interrupting current speech. */ - pub fn speak>(&self, text: S, interrupt: bool) -> Result<&Self, Error> { + pub fn speak>(&mut self, text: S, interrupt: bool) -> Result<&Self, Error> { self.0.speak(text.into().as_str(), interrupt)?; Ok(self) } @@ -139,7 +139,7 @@ impl TTS { /** * Stops current speech. */ - pub fn stop(&self) -> Result<&Self, Error> { + pub fn stop(&mut self) -> Result<&Self, Error> { let Features { stop, .. } = self.supported_features(); if stop { self.0.stop()?;