mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Merge pull request #127 from kingosticks/fix/skipping-with-queued-tracks
Improved next/prev handling for queued tracks. (v2)
This commit is contained in:
commit
e80b97e7b7
1 changed files with 40 additions and 14 deletions
54
src/spirc.rs
54
src/spirc.rs
|
@ -514,35 +514,61 @@ impl SpircTask {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_next(&mut self) {
|
fn consume_queued_track(&mut self) -> usize {
|
||||||
let current_index = self.state.get_playing_track_index();
|
// Removes current track if it is queued
|
||||||
let num_tracks = self.state.get_track().len() as u32;
|
// Returns the index of the next track
|
||||||
let new_index = (current_index + 1) % num_tracks;
|
let current_index = self.state.get_playing_track_index() as usize;
|
||||||
|
if self.state.get_track()[current_index].get_queued() {
|
||||||
let mut was_last_track = (current_index + 1) >= num_tracks;
|
self.state.mut_track().remove(current_index);
|
||||||
if self.state.get_repeat() {
|
return current_index;
|
||||||
was_last_track = false;
|
}
|
||||||
|
current_index + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_next(&mut self) {
|
||||||
|
let mut new_index = self.consume_queued_track() as u32;
|
||||||
|
let mut continue_playing = true;
|
||||||
|
if new_index >= self.state.get_track().len() as u32 {
|
||||||
|
new_index = 0; // Loop around back to start
|
||||||
|
continue_playing = self.state.get_repeat();
|
||||||
|
}
|
||||||
self.state.set_playing_track_index(new_index);
|
self.state.set_playing_track_index(new_index);
|
||||||
self.state.set_position_ms(0);
|
self.state.set_position_ms(0);
|
||||||
self.state.set_position_measured_at(now_ms() as u64);
|
self.state.set_position_measured_at(now_ms() as u64);
|
||||||
|
|
||||||
self.load_track(!was_last_track);
|
self.load_track(continue_playing);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_prev(&mut self) {
|
fn handle_prev(&mut self) {
|
||||||
// Previous behaves differently based on the position
|
// Previous behaves differently based on the position
|
||||||
// Under 3s it goes to the previous song
|
// Under 3s it goes to the previous song (starts playing)
|
||||||
// Over 3s it seeks to zero
|
// Over 3s it seeks to zero (retains previous play status)
|
||||||
if self.position() < 3000 {
|
if self.position() < 3000 {
|
||||||
|
// Queued tracks always follow the currently playing track.
|
||||||
|
// They should not be considered when calculating the previous
|
||||||
|
// track so extract them beforehand and reinsert them after it.
|
||||||
|
let mut queue_tracks = Vec::new();
|
||||||
|
{
|
||||||
|
let queue_index = self.consume_queued_track();
|
||||||
|
let tracks = self.state.mut_track();
|
||||||
|
while queue_index < tracks.len() && tracks[queue_index].get_queued() {
|
||||||
|
queue_tracks.push(tracks.remove(queue_index));
|
||||||
|
}
|
||||||
|
}
|
||||||
let current_index = self.state.get_playing_track_index();
|
let current_index = self.state.get_playing_track_index();
|
||||||
|
let new_index = if current_index > 0 {
|
||||||
let new_index = if current_index == 0 {
|
current_index - 1
|
||||||
|
} else if self.state.get_repeat() {
|
||||||
self.state.get_track().len() as u32 - 1
|
self.state.get_track().len() as u32 - 1
|
||||||
} else {
|
} else {
|
||||||
current_index - 1
|
0
|
||||||
};
|
};
|
||||||
|
// Reinsert queued tracks after the new playing track.
|
||||||
|
let mut pos = (new_index + 1) as usize;
|
||||||
|
for track in queue_tracks.into_iter() {
|
||||||
|
self.state.mut_track().insert(pos, track);
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
self.state.set_playing_track_index(new_index);
|
self.state.set_playing_track_index(new_index);
|
||||||
self.state.set_position_ms(0);
|
self.state.set_position_ms(0);
|
||||||
|
|
Loading…
Reference in a new issue