mirror of
https://github.com/ndarilek/tts-rs.git
synced 2024-11-23 06:49:38 +00:00
Don't re-initialize a player, just clear the item list.
This commit is contained in:
parent
d3e05b5a7a
commit
9c98026978
|
@ -57,101 +57,74 @@ impl WinRT {
|
||||||
player.set_source(&playback_list)?;
|
player.set_source(&playback_list)?;
|
||||||
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
|
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
|
||||||
let bid = BackendId::WinRT(*backend_id);
|
let bid = BackendId::WinRT(*backend_id);
|
||||||
let mut rv = Self {
|
|
||||||
id: bid,
|
|
||||||
synth: SpeechSynthesizer::new()?,
|
|
||||||
player,
|
|
||||||
playback_list,
|
|
||||||
};
|
|
||||||
*backend_id += 1;
|
*backend_id += 1;
|
||||||
Self::init_callbacks(&mut rv)?;
|
|
||||||
Ok(rv)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)?;
|
|
||||||
self.init_callbacks()?;
|
|
||||||
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
||||||
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
||||||
let callbacks = callbacks.get_mut(&self.id).unwrap();
|
|
||||||
if let Some(callback) = callbacks.utterance_stop.as_mut() {
|
|
||||||
for mapping in &*mappings {
|
|
||||||
callback(mapping.2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mappings.retain(|v| v.0 != self.id);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_callbacks(&mut self) -> Result<(), winrt::Error> {
|
|
||||||
let id = self.id().unwrap();
|
|
||||||
let mut backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
|
let mut backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
|
||||||
backend_to_media_player.insert(id, self.player.clone());
|
backend_to_media_player.insert(bid, player.clone());
|
||||||
self.player
|
player.media_ended(TypedEventHandler::new(|sender, _args| {
|
||||||
.media_ended(TypedEventHandler::new(|sender, _args| {
|
let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
|
||||||
let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
|
let id = backend_to_media_player.iter().find(|v| v.1 == sender);
|
||||||
let id = backend_to_media_player.iter().find(|v| v.1 == sender);
|
if let Some(id) = id {
|
||||||
|
let id = id.0;
|
||||||
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||||
|
let callbacks = callbacks.get_mut(&id).unwrap();
|
||||||
|
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
||||||
|
let last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
|
||||||
|
if let Some(utterance_id) = last_spoken_utterance.get(&id) {
|
||||||
|
callback(utterance_id.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}))?;
|
||||||
|
let mut backend_to_playback_list = BACKEND_TO_PLAYBACK_LIST.lock().unwrap();
|
||||||
|
backend_to_playback_list.insert(bid, playback_list.clone());
|
||||||
|
playback_list.current_item_changed(TypedEventHandler::new(
|
||||||
|
|sender: &MediaPlaybackList, args: &CurrentMediaPlaybackItemChangedEventArgs| {
|
||||||
|
let backend_to_playback_list = BACKEND_TO_PLAYBACK_LIST.lock().unwrap();
|
||||||
|
let id = backend_to_playback_list.iter().find(|v| v.1 == sender);
|
||||||
if let Some(id) = id {
|
if let Some(id) = id {
|
||||||
let id = id.0;
|
let id = id.0;
|
||||||
let mut callbacks = CALLBACKS.lock().unwrap();
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||||
let callbacks = callbacks.get_mut(&id).unwrap();
|
let callbacks = callbacks.get_mut(&id).unwrap();
|
||||||
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
let old_item = args.old_item()?;
|
||||||
let last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
|
if !old_item.is_null() {
|
||||||
if let Some(utterance_id) = last_spoken_utterance.get(&id) {
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
||||||
callback(utterance_id.clone());
|
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
||||||
|
for mapping in &*mappings {
|
||||||
|
if mapping.1 == old_item {
|
||||||
|
callback(mapping.2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mappings.retain(|v| v.1 != old_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let new_item = args.new_item()?;
|
||||||
|
if !new_item.is_null() {
|
||||||
|
let mut last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
|
||||||
|
let mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
||||||
|
for mapping in &*mappings {
|
||||||
|
if mapping.1 == new_item {
|
||||||
|
last_spoken_utterance.insert(*id, mapping.2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(callback) = callbacks.utterance_begin.as_mut() {
|
||||||
|
for mapping in &*mappings {
|
||||||
|
if mapping.1 == new_item {
|
||||||
|
callback(mapping.2);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}))?;
|
},
|
||||||
let mut backend_to_playback_list = BACKEND_TO_PLAYBACK_LIST.lock().unwrap();
|
))?;
|
||||||
backend_to_playback_list.insert(id, self.playback_list.clone());
|
Ok(Self {
|
||||||
self.playback_list
|
id: bid,
|
||||||
.current_item_changed(TypedEventHandler::new(
|
synth: SpeechSynthesizer::new()?,
|
||||||
|sender: &MediaPlaybackList, args: &CurrentMediaPlaybackItemChangedEventArgs| {
|
player,
|
||||||
let backend_to_playback_list = BACKEND_TO_PLAYBACK_LIST.lock().unwrap();
|
playback_list,
|
||||||
let id = backend_to_playback_list.iter().find(|v| v.1 == sender);
|
})
|
||||||
if let Some(id) = id {
|
|
||||||
let id = id.0;
|
|
||||||
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
||||||
let callbacks = callbacks.get_mut(&id).unwrap();
|
|
||||||
let old_item = args.old_item()?;
|
|
||||||
if !old_item.is_null() {
|
|
||||||
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
||||||
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
|
||||||
for mapping in &*mappings {
|
|
||||||
if mapping.1 == old_item {
|
|
||||||
callback(mapping.2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mappings.retain(|v| v.1 != old_item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let new_item = args.new_item()?;
|
|
||||||
if !new_item.is_null() {
|
|
||||||
let mut last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
|
|
||||||
let mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
||||||
for mapping in &*mappings {
|
|
||||||
if mapping.1 == new_item {
|
|
||||||
last_spoken_utterance.insert(*id, mapping.2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(callback) = callbacks.utterance_begin.as_mut() {
|
|
||||||
for mapping in &*mappings {
|
|
||||||
if mapping.1 == new_item {
|
|
||||||
callback(mapping.2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +171,16 @@ impl Backend for WinRT {
|
||||||
|
|
||||||
fn stop(&mut self) -> std::result::Result<(), Error> {
|
fn stop(&mut self) -> std::result::Result<(), Error> {
|
||||||
trace!("stop()");
|
trace!("stop()");
|
||||||
self.reinit_player()?;
|
self.playback_list.items()?.clear()?;
|
||||||
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
||||||
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||||
|
let callbacks = callbacks.get_mut(&self.id).unwrap();
|
||||||
|
if let Some(callback) = callbacks.utterance_stop.as_mut() {
|
||||||
|
for mapping in &*mappings {
|
||||||
|
callback(mapping.2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mappings.retain(|v| v.0 != self.id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user