feat: Enhance play_next functionality by storing and restoring playlist index
This commit is contained in:
@@ -65,6 +65,7 @@ class PlayerState:
|
|||||||
self.playlist_duration: int = 0
|
self.playlist_duration: int = 0
|
||||||
self.playlist_sounds: list[Sound] = []
|
self.playlist_sounds: list[Sound] = []
|
||||||
self.play_next_queue: list[Sound] = []
|
self.play_next_queue: list[Sound] = []
|
||||||
|
self.playlist_index_before_play_next: int | None = None
|
||||||
|
|
||||||
def to_dict(self) -> dict[str, Any]:
|
def to_dict(self) -> dict[str, Any]:
|
||||||
"""Convert player state to dictionary for serialization."""
|
"""Convert player state to dictionary for serialization."""
|
||||||
@@ -353,6 +354,26 @@ class PlayerService:
|
|||||||
await self._play_next_from_queue()
|
await self._play_next_from_queue()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# If currently playing from play_next queue (no index but have stored index)
|
||||||
|
if (
|
||||||
|
self.state.current_sound_index is None
|
||||||
|
and self.state.playlist_index_before_play_next is not None
|
||||||
|
and self.state.playlist_sounds
|
||||||
|
):
|
||||||
|
# Skipped the last play_next track, go to next in playlist
|
||||||
|
restored_index = self.state.playlist_index_before_play_next
|
||||||
|
next_index = self._get_next_index(restored_index)
|
||||||
|
|
||||||
|
# Clear the stored index
|
||||||
|
self.state.playlist_index_before_play_next = None
|
||||||
|
|
||||||
|
if next_index is not None:
|
||||||
|
await self.play(next_index)
|
||||||
|
else:
|
||||||
|
await self._stop_playback()
|
||||||
|
await self._broadcast_state()
|
||||||
|
return
|
||||||
|
|
||||||
if not self.state.playlist_sounds:
|
if not self.state.playlist_sounds:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -467,6 +488,20 @@ class PlayerService:
|
|||||||
if not self.state.play_next_queue:
|
if not self.state.play_next_queue:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Store current playlist index before switching to play_next track
|
||||||
|
# Only store if we're currently playing from the playlist
|
||||||
|
if (
|
||||||
|
self.state.current_sound_index is not None
|
||||||
|
and self.state.playlist_index_before_play_next is None
|
||||||
|
):
|
||||||
|
self.state.playlist_index_before_play_next = (
|
||||||
|
self.state.current_sound_index
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Stored playlist index %s before playing from play_next queue",
|
||||||
|
self.state.playlist_index_before_play_next,
|
||||||
|
)
|
||||||
|
|
||||||
# Get the first sound from the queue
|
# Get the first sound from the queue
|
||||||
next_sound = self.state.play_next_queue.pop(0)
|
next_sound = self.state.play_next_queue.pop(0)
|
||||||
|
|
||||||
@@ -581,6 +616,11 @@ class PlayerService:
|
|||||||
logger.info("Clearing play_next queue due to playlist change")
|
logger.info("Clearing play_next queue due to playlist change")
|
||||||
self.state.play_next_queue.clear()
|
self.state.play_next_queue.clear()
|
||||||
|
|
||||||
|
# Clear stored playlist index
|
||||||
|
if self.state.playlist_index_before_play_next is not None:
|
||||||
|
logger.info("Clearing stored playlist index due to playlist change")
|
||||||
|
self.state.playlist_index_before_play_next = None
|
||||||
|
|
||||||
if self.state.status != PlayerStatus.STOPPED:
|
if self.state.status != PlayerStatus.STOPPED:
|
||||||
await self._stop_playback()
|
await self._stop_playback()
|
||||||
|
|
||||||
@@ -881,9 +921,29 @@ class PlayerService:
|
|||||||
else:
|
else:
|
||||||
await self._stop_playback()
|
await self._stop_playback()
|
||||||
await self._broadcast_state()
|
await self._broadcast_state()
|
||||||
elif self.state.playlist_sounds:
|
elif (
|
||||||
# Current track was from play_next, go to playlist
|
self.state.playlist_sounds
|
||||||
await self.play(0)
|
and self.state.playlist_index_before_play_next is not None
|
||||||
|
):
|
||||||
|
# Current track was from play_next queue, restore to next track in playlist
|
||||||
|
restored_index = self.state.playlist_index_before_play_next
|
||||||
|
logger.info(
|
||||||
|
"Play next queue finished, continuing from playlist index %s",
|
||||||
|
restored_index,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get the next index based on the stored position
|
||||||
|
next_index = self._get_next_index(restored_index)
|
||||||
|
|
||||||
|
# Clear the stored index since we're done with play_next queue
|
||||||
|
self.state.playlist_index_before_play_next = None
|
||||||
|
|
||||||
|
if next_index is not None:
|
||||||
|
await self.play(next_index)
|
||||||
|
else:
|
||||||
|
# No next track (end of playlist in non-loop mode)
|
||||||
|
await self._stop_playback()
|
||||||
|
await self._broadcast_state()
|
||||||
else:
|
else:
|
||||||
await self._stop_playback()
|
await self._stop_playback()
|
||||||
await self._broadcast_state()
|
await self._broadcast_state()
|
||||||
|
|||||||
Reference in New Issue
Block a user