Skip to content

Commit

Permalink
Adapt newer version of limited-queue and change structure of `Activ…
Browse files Browse the repository at this point in the history
…eSong`
  • Loading branch information
Shiritai committed Jul 12, 2024
1 parent 4140826 commit bb2fdc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ description = "An out-of-the-box rust audio-player library that utilize rodio in
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
limited-queue = "0.1.0"
limited-queue = "0.1.2"
rodio = "0.19.0"
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum PlaybackState {

#[derive(Clone, Default, Debug)]
pub struct ActiveSong {
song: Song,
song: Option<Song>,
state: PlaybackState,
progress: Duration,
duration: Duration,
Expand All @@ -53,7 +53,7 @@ pub struct ActiveSong {
impl ActiveSong {
pub fn from(song: Song, duration: Duration) -> ActiveSong {
ActiveSong {
song,
song: Some(song),
state: PlaybackState::NONE,
progress: Duration::from_secs(0),
duration,
Expand Down Expand Up @@ -188,6 +188,7 @@ impl Player for SharedPlayer {
let mut state = state.write().unwrap();
state.current.progress = state.current.duration;
state.current.state = PlaybackState::STOP;
state.current.song = None;
state.played_q.push(song.clone());
}
{
Expand Down Expand Up @@ -231,7 +232,9 @@ impl Player for SharedPlayer {
fn clear(&self) {
// acquire an arc for this thread
let state = Arc::clone(&self);
state.write().unwrap().waiting_q.clear();
let mut state = state.write().unwrap();
state.waiting_q.clear();
state.played_q.clear();
}

fn is_playing(&self) -> bool {
Expand Down Expand Up @@ -352,14 +355,24 @@ mod tests {
#[test]
fn test_stop() {
let player = SharedPlayer::make();
for _ in 0..100 {
for _ in 0..10 {
player.add(Song::from("Music".into(), "audio/short_sound".into()));
}
player.use_auto_play();

let t = player.play();
sleep(Duration::from_secs(3));
player.stop();
let _ = t.join(); // should stop immediately


sleep(Duration::from_secs(1));
for _ in 0..10 {
player.add(Song::from("Music".into(), "audio/short_sound".into()));
}

let t = player.play();
sleep(Duration::from_secs(3));
let _ = t.join(); // should stop immediately
}
}

0 comments on commit bb2fdc7

Please sign in to comment.