From 55812893517cc8b5a39d8582ce8bf66570022421 Mon Sep 17 00:00:00 2001 From: nsteel Date: Fri, 27 Oct 2017 18:45:02 +0100 Subject: [PATCH 1/2] Added repeat support --- src/spirc.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/spirc.rs b/src/spirc.rs index 3b5fb77c..06f20635 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -396,6 +396,11 @@ impl SpircTask { self.notify(None); } + MessageType::kMessageTypeRepeat => { + self.state.set_repeat(frame.get_state().get_repeat()); + self.notify(None); + } + MessageType::kMessageTypeSeek => { let position = frame.get_position(); @@ -467,13 +472,19 @@ impl SpircTask { fn handle_next(&mut self) { let current_index = self.state.get_playing_track_index(); - let new_index = (current_index + 1) % (self.state.get_track().len() as u32); + let num_tracks = self.state.get_track().len() as u32; + let new_index = (current_index + 1) % num_tracks; + + let mut was_last_track = (current_index + 1) >= num_tracks; + if self.state.get_repeat() { + was_last_track = false; + } self.state.set_playing_track_index(new_index); self.state.set_position_ms(0); self.state.set_position_measured_at(now_ms() as u64); - self.load_track(true); + self.load_track(!was_last_track); } fn handle_prev(&mut self) { @@ -520,14 +531,7 @@ impl SpircTask { } fn handle_end_of_track(&mut self) { - let current_index = self.state.get_playing_track_index(); - let new_index = (current_index + 1) % (self.state.get_track().len() as u32); - - self.state.set_playing_track_index(new_index); - self.state.set_position_ms(0); - self.state.set_position_measured_at(now_ms() as u64); - - self.load_track(true); + self.handle_next(); self.notify(None); } From 628df27292e08d44400e4639a8ef25f50ecbb804 Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Fri, 3 Nov 2017 01:15:27 +0000 Subject: [PATCH 2/2] Support for enabling shuffle. --- src/spirc.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/spirc.rs b/src/spirc.rs index 06f20635..750badc8 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -17,6 +17,9 @@ use protocol::spirc::{PlayStatus, State, MessageType, Frame, DeviceState}; use mixer::Mixer; use player::Player; +use rand; +use rand::Rng; + pub struct SpircTask { player: Player, mixer: Box, @@ -401,6 +404,26 @@ impl SpircTask { self.notify(None); } + MessageType::kMessageTypeShuffle => { + self.state.set_shuffle(frame.get_state().get_shuffle()); + if self.state.get_shuffle() + { + let current_index = self.state.get_playing_track_index(); + { + let tracks = self.state.mut_track(); + tracks.swap(0, current_index as usize); + if let Some((_, rest)) = tracks.split_first_mut() { + rand::thread_rng().shuffle(rest); + } + } + self.state.set_playing_track_index(0); + } else { + let context = self.state.get_context_uri(); + debug!("{:?}", context); + } + self.notify(None); + } + MessageType::kMessageTypeSeek => { let position = frame.get_position();