diff --git a/jukebox/jukebox.go b/jukebox/jukebox.go index a0cc5ebb..ecab58a9 100644 --- a/jukebox/jukebox.go +++ b/jukebox/jukebox.go @@ -121,7 +121,7 @@ func (j *Jukebox) SetPlaylist(items []string) error { if err := getDecode(j.conn, &playlist, "playlist"); err != nil { return fmt.Errorf("get playlist: %w", err) } - currentPlaylistIndex := slices.IndexFunc(playlist, func(pl mpvPlaylistItem) bool { + currentPlayingIndex := slices.IndexFunc(playlist, func(pl mpvPlaylistItem) bool { return pl.Current }) @@ -131,19 +131,18 @@ func (j *Jukebox) SetPlaylist(items []string) error { } defer cleanup() - var foundExistingTrack bool - for _, item := range items { + var newPlayingIndex int = -1 + for i, item := range items { item, _ = filepath.Abs(item) - - if currentPlaylistIndex >= 0 && playlist[currentPlaylistIndex].Filename == item { - foundExistingTrack = true + if currentPlayingIndex >= 0 && playlist[currentPlayingIndex].Filename == item { + newPlayingIndex = i continue // don't add current track to loadlist } fmt.Fprintln(tmp, item) } - if !foundExistingTrack { + if newPlayingIndex < 0 { if _, err := j.conn.Call("loadlist", tmp.Name(), "replace"); err != nil { return fmt.Errorf("load list: %w", err) } @@ -156,6 +155,11 @@ func (j *Jukebox) SetPlaylist(items []string) error { if _, err := j.conn.Call("loadlist", tmp.Name(), "append-play"); err != nil { return fmt.Errorf("load list: %w", err) } + if newPlayingIndex > 0 { + if _, err := j.conn.Call("playlist-move", 0, newPlayingIndex+1); err != nil { + return fmt.Errorf("playlist move: %w", err) + } + } return nil } diff --git a/jukebox/jukebox_test.go b/jukebox/jukebox_test.go index f38fef4c..f7716780 100644 --- a/jukebox/jukebox_test.go +++ b/jukebox/jukebox_test.go @@ -74,7 +74,7 @@ func TestPlaySkipReset(t *testing.T) { require.Equal(t, true, status.Playing) // just add one more by overwriting the playlist like some clients do - // we should move the now play playing index (3) back to the start if we find it + // we should move keep the playing indedx require.NoError(t, j.SetPlaylist([]string{ "testdata/tr_0.mp3", "testdata/tr_1.mp3", @@ -86,21 +86,11 @@ func TestPlaySkipReset(t *testing.T) { status, err = j.GetStatus() require.NoError(t, err) - require.Equal(t, 0, status.CurrentIndex) // index moved to start + require.Equal(t, 3, status.CurrentIndex) // index moved to start require.Equal(t, testPath("tr_3.mp3"), status.CurrentFilename) require.Equal(t, 6, status.Length) // we added one more track require.Equal(t, true, status.Playing) - // skip to index 3 again, which is now tr_2 after re-order - require.NoError(t, j.SkipToPlaylistIndex(3, 0)) - - status, err = j.GetStatus() - require.NoError(t, err) - require.Equal(t, 3, status.CurrentIndex) - require.Equal(t, testPath("tr_2.mp3"), status.CurrentFilename) - require.Equal(t, 6, status.Length) - require.Equal(t, true, status.Playing) - // new playlist with out current track (tr_2) require.NoError(t, j.SetPlaylist([]string{ "testdata/tr_6.mp3",