pause, unpause, get_state ok

This commit is contained in:
Justine 2024-08-12 16:05:55 +02:00
parent 123f41930d
commit f388ce5152
2 changed files with 43 additions and 21 deletions

View File

@ -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<dyn Error>> {
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<dyn Error>> {
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!();
}

View File

@ -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));
}
}