From c6b960466b767c2d80a41d892b4c958a9dc71e55 Mon Sep 17 00:00:00 2001 From: unrenowned <163776820+unrenowned@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:27:18 +0000 Subject: [PATCH 1/2] playback: fix playNext crash on last song of queue Fixes OxygenCobalt/Auxio#735. ExoPlayer method for fetching next media item returns C.INDEX_UNSET (-1) when used on the last song of a queue, which is not a valid index for ExoPlayer.addMediaItems(). New code just adds songs to the end of the queue if there isn't a next song. --- .../org/oxycblt/auxio/playback/system/PlaybackService.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt index 0250a6a85..997e355a9 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt @@ -377,7 +377,14 @@ class PlaybackService : } override fun playNext(songs: List, ack: StateAck.PlayNext) { - player.addMediaItems(player.nextMediaItemIndex, songs.map { it.toMediaItem() }) + val nextIndex = player.nextMediaItemIndex + + if (nextIndex == C.INDEX_UNSET) { + player.addMediaItems(songs.map { it.toMediaItem() }) + } else { + player.addMediaItems(nextIndex, songs.map { it.toMediaItem() }) + } + playbackManager.ack(this, ack) deferSave() } From 1d9dc345494c59e82eb77894631c979726e856de Mon Sep 17 00:00:00 2001 From: unrenowned <163776820+unrenowned@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:40:42 +0000 Subject: [PATCH 2/2] playback: fix playNext wraparound with Repeat All ExoPlayer method for fetching next media item respects Repeat All, which on the last song of a queue causes playNext to wrap around and insert the songs at the start of the queue. New code fetches next song as if repeat were turned off, so the songs will always be added to the end of the queue. --- .../oxycblt/auxio/playback/system/PlaybackService.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt index 997e355a9..19b7136a8 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt @@ -377,7 +377,17 @@ class PlaybackService : } override fun playNext(songs: List, ack: StateAck.PlayNext) { - val nextIndex = player.nextMediaItemIndex + val currTimeline = player.currentTimeline + val nextIndex = + if (currTimeline.isEmpty) { + C.INDEX_UNSET + } else { + currTimeline.getNextWindowIndex( + player.currentMediaItemIndex, + Player.REPEAT_MODE_OFF, + player.shuffleModeEnabled + ) + } if (nextIndex == C.INDEX_UNSET) { player.addMediaItems(songs.map { it.toMediaItem() })