Moved location of uri.exists() to in Player

This commit is contained in:
G2-Games 2024-02-17 21:30:53 -06:00
parent c6b561c7af
commit d2822bbb37
2 changed files with 14 additions and 7 deletions

View file

@ -249,9 +249,7 @@ impl Controller {
in_thread.send(QueueResponse::Default).unwrap(); in_thread.send(QueueResponse::Default).unwrap();
}, },
Enqueue(uri) => { Enqueue(uri) => {
if uri.exists().unwrap() { queue.player.enqueue_next(&uri).unwrap();
queue.player.enqueue_next(&uri);
}
// in_thread.send(QueueResponse::Default).unwrap(); // in_thread.send(QueueResponse::Default).unwrap();
} }

View file

@ -68,6 +68,8 @@ pub enum PlayerError {
Factory(#[from] glib::BoolError), Factory(#[from] glib::BoolError),
#[error("could not change playback state")] #[error("could not change playback state")]
StateChange(#[from] gst::StateChangeError), StateChange(#[from] gst::StateChangeError),
#[error("the file or source is not found")]
NotFound,
#[error("failed to build gstreamer item")] #[error("failed to build gstreamer item")]
Build, Build,
#[error("poison error")] #[error("poison error")]
@ -257,12 +259,17 @@ impl Player {
&self.source &self.source
} }
pub fn enqueue_next(&mut self, next_track: &URI) { pub fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError> {
self.set_source(next_track); self.set_source(next_track)
} }
/// Set the playback URI /// 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 // Make sure the playback tracker knows the stuff is stopped
self.playback_tx.send(PlaybackStats::Switching).unwrap(); self.playback_tx.send(PlaybackStats::Switching).unwrap();
@ -290,7 +297,7 @@ impl Player {
let now = std::time::Instant::now(); let now = std::time::Instant::now();
while now.elapsed() < std::time::Duration::from_millis(20) { while now.elapsed() < std::time::Duration::from_millis(20) {
if self.seek_to(Duration::from_std(*start).unwrap()).is_ok() { if self.seek_to(Duration::from_std(*start).unwrap()).is_ok() {
return; return Ok(());
} }
std::thread::sleep(std::time::Duration::from_millis(1)); std::thread::sleep(std::time::Duration::from_millis(1));
} }
@ -321,6 +328,8 @@ impl Player {
}).unwrap(); }).unwrap();
} }
} }
Ok(())
} }
/// Gets a mutable reference to the playbin element /// Gets a mutable reference to the playbin element