Fix enqueue next if player is already playing
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 18 Feb 2025 16:29:51 +0000 (17:29 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 18 Feb 2025 16:29:51 +0000 (17:29 +0100)
music_assistant/controllers/player_queues.py

index 8a2fcc730da9ec34a1288b12cffb026174a580fd..93459cfb87bde36ad822355991f99fdcd110785e 100644 (file)
@@ -463,7 +463,7 @@ class PlayerQueuesController(CoreController):
 
         # load the items into the queue
         if queue.state in (PlayerState.PLAYING, PlayerState.PAUSED):
-            cur_index = queue.index_in_buffer or 0
+            cur_index = queue.index_in_buffer or queue.current_index or 0
         else:
             cur_index = queue.current_index or 0
         insert_at_index = cur_index + 1 if self._queue_items.get(queue_id) else 0
@@ -1090,6 +1090,7 @@ class PlayerQueuesController(CoreController):
         - keep_remaining: keep the remaining items after the insert
         - shuffle: (re)shuffle the items after insert index
         """
+        queue = self._queues[queue_id]
         prev_items = self._queue_items[queue_id][:insert_at_index] if keep_played else []
         next_items = queue_items
 
@@ -1105,6 +1106,23 @@ class PlayerQueuesController(CoreController):
             next_items = random.sample(next_items, len(next_items))
         self.update_items(queue_id, prev_items + next_items)
 
+        # if the next index changed we need to tell the player to enqueue the (new) next item
+        index_in_buffer = queue.index_in_buffer or queue.current_index or 0
+        if (
+            insert_at_index == (index_in_buffer + 1)
+            and queue.state != PlayerState.IDLE
+            and not queue.flow_mode
+            and (current_item_in_buffer := self.get_item(queue_id, index_in_buffer))
+        ):
+            task_id = f"enqueue_next_item_{queue_id}"
+            self.mass.call_later(
+                1,
+                self._enqueue_next_item,
+                queue_id,
+                current_item_in_buffer.queue_item_id,
+                task_id=task_id,
+            )
+
     def update_items(self, queue_id: str, queue_items: list[QueueItem]) -> None:
         """Update the existing queue items, mostly caused by reordering."""
         self._queue_items[queue_id] = queue_items