diff --git a/src/audioplayer.rs b/src/audioplayer.rs index 7914a22..49103f6 100644 --- a/src/audioplayer.rs +++ b/src/audioplayer.rs @@ -47,7 +47,7 @@ pub mod audioplayer { //-----------------------------------Enums ///Returned by player.get_state() - #[derive(Debug, Clone)] + #[derive(Debug, Clone, PartialEq)] pub enum PlayerState { Playing, Paused, @@ -77,15 +77,16 @@ pub mod audioplayer { } ///Plays all files in the playlist starting from index. + ///Anything that was previously in the sink is removed. pub fn play_at(&mut self, index: usize) -> Result<(), Box> { self.playlist.check_index(index.clone())?; - self.sink.clear(); + self.sink.stop(); 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.sink.play(); return Ok(()); } @@ -112,32 +113,62 @@ pub mod audioplayer { return PlayerState::Stopped; } + ///Pauses playing if applicable, otherwise nothing. pub fn pause(&mut self) { self.sink.pause(); } + ///Resumes a paused player if applicable, otherwise nothing. pub fn unpause(&mut self) { self.sink.play(); } - pub fn skip_forward(&mut self) -> Result<(), Box> { - unimplemented!(); + ///Skips to the next song in the playlist, otherwise ends. + pub fn skip_forward(&mut self) { + self.sink.skip_one(); } + ///Skips back a song if applicable, otherwise does nothing. pub fn skip_backwards(&mut self) { - unimplemented!(); + + //Are we currently playing ? + if self.get_state() == PlayerState::Stopped { + return (); + } + + //Are we able to get the current song and is at least the 2nd one ? + let mut index: usize = 1; + if let Some(curr) = self.get_current_song() { + index = curr.1.clone(); + if index < 1 { + return (); + } + } else { + return (); + } + + //Everything OK, we can skip. + self.sink.clear(); + self.play_at(index -1); } - pub fn get_pos(&self) { - unimplemented!(); + ///Returns current position in the song, if playing. + pub fn get_pos(&self) -> Option { + if self.get_state() == PlayerState::Playing { + return Some(self.sink.get_pos()); + } + return None; } + ///Takes a volume and sets the player to it. + ///1.0 corresponds to 100% - anything over that might be too loud ! pub fn set_vol(&mut self, vol: f32) { - unimplemented!(); + &self.sink.set_volume(vol); } - pub fn get_vol(&mut self) { - unimplemented!(); + ///Returns the volume of the player + pub fn get_vol(&mut self) -> f32{ + self.sink.volume() } pub fn goto(&mut self, index: usize) { diff --git a/src/main.rs b/src/main.rs index 7abc37b..d47dd13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,14 +29,23 @@ fn main() { dbg!(player.get_state()); sleep(Duration::from_secs(3)); - player.pause(); - dbg!(player.get_state()); + dbg!("SKIPPING FORW TWICE"); + player.skip_forward(); + player.skip_forward(); sleep(Duration::from_secs(3)); + dbg!("BACKW 5"); + player.skip_backwards(); + player.skip_backwards(); + player.skip_backwards(); + player.skip_backwards(); + player.skip_backwards(); + player.unpause(); + player.set_vol(0.9); loop { - println!("{:?} - {:?}", player.get_state(), player.get_current_song()); + println!("{:?} - {:?}", player.get_state(), player.get_vol()); sleep(Duration::from_secs(1)); }