From 1eafc38b0d4be8272621a4f8cb52a77179d825fa Mon Sep 17 00:00:00 2001 From: Shaowen Yin Date: Sun, 28 Jul 2024 23:41:42 +0800 Subject: [PATCH] player: fix next_song when the following songs are bad (#858) Before, next_song returns no song when the following songs are bad. For example, the playlist has [valid_song1, valid_song2, bad_song1, bad_song2] and the current song is valid_song2. At the this time, next_song return None before. After this PR, next_song returns valid_song1. --- feeluown/player/playlist.py | 15 +++++++++------ tests/player/test_playlist.py | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/feeluown/player/playlist.py b/feeluown/player/playlist.py index 82b3910ce..9da5edc3f 100644 --- a/feeluown/player/playlist.py +++ b/feeluown/player/playlist.py @@ -381,13 +381,16 @@ def next_song(self): next_song = self._get_good_song(random_=True) else: current_index = self._songs.index(self.current_song) - if current_index == len(self._songs) - 1: - if self.playback_mode in (PlaybackMode.loop, PlaybackMode.one_loop): - next_song = self._get_good_song() - elif self.playback_mode == PlaybackMode.sequential: - next_song = None + is_last_song = current_index == len(self._songs) - 1 + if is_last_song and self.playback_mode == PlaybackMode.sequential: + next_song = None else: - next_song = self._get_good_song(base=current_index+1, loop=False) + if is_last_song: + base_index = 0 + else: + base_index = current_index + 1 + loop = self.playback_mode != PlaybackMode.sequential + next_song = self._get_good_song(base=base_index, loop=loop) return next_song @property diff --git a/tests/player/test_playlist.py b/tests/player/test_playlist.py index 791e8c561..c80e06348 100644 --- a/tests/player/test_playlist.py +++ b/tests/player/test_playlist.py @@ -366,3 +366,10 @@ class Album: # app_mock.library.album_upgrade.return_value = album # When cover is a media object, prepare_metadata should also succeed. await pl._metadata_mgr.prepare_for_song(ekaf_brief_song0) + + +def test_playlist_next_song(pl): + pl.mark_as_bad(pl.list()[1]) + assert pl.next_song == pl.list()[0] + pl.playback_mode = PlaybackMode.sequential + assert pl.next_song is None