From f388ce5152e073bffd4681786903a64b2ce2b40a Mon Sep 17 00:00:00 2001 From: Justine Date: Mon, 12 Aug 2024 16:05:55 +0200 Subject: [PATCH] pause, unpause, get_state ok --- src/audioplayer.rs | 40 ++++++++++++++++++++++++---------------- src/main.rs | 24 +++++++++++++++++++----- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/audioplayer.rs b/src/audioplayer.rs index 21d017e..7914a22 100644 --- a/src/audioplayer.rs +++ b/src/audioplayer.rs @@ -42,13 +42,12 @@ pub mod audioplayer { pub playlist: Playlist, ///If true, the player will loop back to the beginning of the playlist when it's done pub repeating: bool, - ///Stores the current state of the player, can be used as a source of truth for an UI. - pub state: PlayerState, } //-----------------------------------Enums - #[derive(Debug)] + ///Returned by player.get_state() + #[derive(Debug, Clone)] pub enum PlayerState { Playing, Paused, @@ -73,7 +72,6 @@ pub mod audioplayer { stream: stream, playlist: playlist, repeating: false, - state: PlayerState::Stopped, }; Ok(me) } @@ -82,21 +80,20 @@ pub mod audioplayer { pub fn play_at(&mut self, index: usize) -> Result<(), Box> { self.playlist.check_index(index.clone())?; self.sink.clear(); - self.state = PlayerState::Paused; for song in &self.playlist.songs[index..] { let file = BufReader::new(File::open(&song.path)?); let source = Decoder::new(file)?; self.sink.append(source); } self.sink.play(); - self.state = PlayerState::Playing; return Ok(()); } ///Returns a copy of the current song and its index in the playlist ///Or None if the playlist's empty pub fn get_current_song(&self) -> Option<(SongMeta, usize)> { - if self.playlist.songs.len() == 0 { + if self.playlist.songs.len() == 0 || + self.sink.len() == 0 { return None; } let index: usize = self.playlist.songs.len() - self.sink.len(); @@ -104,7 +101,26 @@ pub mod audioplayer { return Some((songcopy, index)); } - pub fn skip_forward(&mut self) { + ///Returns the current state of the player + pub fn get_state(&self) -> PlayerState { + if self.sink.is_paused() { + return PlayerState::Paused; + } + if self.sink.len() > 0 { + return PlayerState::Playing; + } + return PlayerState::Stopped; + } + + pub fn pause(&mut self) { + self.sink.pause(); + } + + pub fn unpause(&mut self) { + self.sink.play(); + } + + pub fn skip_forward(&mut self) -> Result<(), Box> { unimplemented!(); } @@ -112,14 +128,6 @@ pub mod audioplayer { unimplemented!(); } - pub fn pause(&mut self) { - unimplemented!(); - } - - pub fn unpause(&mut self) { - unimplemented!(); - } - pub fn get_pos(&self) { unimplemented!(); } diff --git a/src/main.rs b/src/main.rs index 29e286b..7abc37b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,9 +11,10 @@ use std::thread::sleep; use std::time::Duration; fn main() { - let mysong = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/A_classer/Bleach/12 Big Cheese.mp3")).unwrap(); - let mysong2 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 01 Dods....flac")).unwrap(); - let mysong3 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Jeux/Superliminal/Matt Christensen - Superliminal/12. Astral.mp3")).unwrap(); + let mysong = SongMeta::frompath(&String::from("/home/justine/Music/one.mp3")).unwrap(); + let mysong2 = SongMeta::frompath(&String::from("/home/justine/Music/two.mp3")).unwrap(); + let mysong3 = SongMeta::frompath(&String::from("/home/justine/Music/three.mp3")).unwrap(); + let mut playlist = Playlist::new(); playlist.songs.push(mysong); @@ -23,8 +24,21 @@ fn main() { let mut player = AudioPlayer::new().unwrap(); - player.playlist = Playlist::new(); - dbg!(player.get_current_song()); + player.playlist = playlist; + player.play_at(0); + dbg!(player.get_state()); + sleep(Duration::from_secs(3)); + + player.pause(); + dbg!(player.get_state()); + sleep(Duration::from_secs(3)); + + player.unpause(); + + loop { + println!("{:?} - {:?}", player.get_state(), player.get_current_song()); + sleep(Duration::from_secs(1)); + } }