From d2822bbb37a3ffb772e4c3f161a21e4dc2b2ab97 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Sat, 17 Feb 2024 21:30:53 -0600 Subject: [PATCH] Moved location of `uri.exists()` to in `Player` --- src/music_controller/controller.rs | 4 +--- src/music_player.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/music_controller/controller.rs b/src/music_controller/controller.rs index 750ee02..d6d1398 100644 --- a/src/music_controller/controller.rs +++ b/src/music_controller/controller.rs @@ -249,9 +249,7 @@ impl Controller { in_thread.send(QueueResponse::Default).unwrap(); }, Enqueue(uri) => { - if uri.exists().unwrap() { - queue.player.enqueue_next(&uri); - } + queue.player.enqueue_next(&uri).unwrap(); // in_thread.send(QueueResponse::Default).unwrap(); } diff --git a/src/music_player.rs b/src/music_player.rs index 4e7de23..12af300 100644 --- a/src/music_player.rs +++ b/src/music_player.rs @@ -68,6 +68,8 @@ pub enum PlayerError { Factory(#[from] glib::BoolError), #[error("could not change playback state")] StateChange(#[from] gst::StateChangeError), + #[error("the file or source is not found")] + NotFound, #[error("failed to build gstreamer item")] Build, #[error("poison error")] @@ -257,12 +259,17 @@ impl Player { &self.source } - pub fn enqueue_next(&mut self, next_track: &URI) { - self.set_source(next_track); + pub fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError> { + self.set_source(next_track) } /// Set the playback URI - fn set_source(&mut self, source: &URI) { + fn set_source(&mut self, source: &URI) -> Result<(), PlayerError> { + if !source.exists().is_ok_and(|x| x) { + // If the source doesn't exist, gstreamer will crash! + return Err(PlayerError::NotFound) + } + // Make sure the playback tracker knows the stuff is stopped self.playback_tx.send(PlaybackStats::Switching).unwrap(); @@ -290,7 +297,7 @@ impl Player { let now = std::time::Instant::now(); while now.elapsed() < std::time::Duration::from_millis(20) { if self.seek_to(Duration::from_std(*start).unwrap()).is_ok() { - return; + return Ok(()); } std::thread::sleep(std::time::Duration::from_millis(1)); } @@ -321,6 +328,8 @@ impl Player { }).unwrap(); } } + + Ok(()) } /// Gets a mutable reference to the playbin element