Skip to content

Commit 316bb43

Browse files
committed
Handle shuffle when lazily seeking to next/previous media items
1 parent 4399b85 commit 316bb43

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/SimpleMusicPlayer.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,34 @@ class SimpleMusicPlayer(private val exoPlayer: ExoPlayer) : ForwardingPlayer(exo
142142
seekJob?.cancel()
143143
seekJob = scope.launch {
144144
delay(timeMillis = 400)
145-
if (seekToNextCount > 0 || seekToPreviousCount > 0) {
146-
runOnPlayerThread {
147-
if (currentMediaItem != null) {
148-
if (seekToNextCount > 0) {
149-
seekTo(rotateIndex(currentMediaItemIndex + seekToNextCount), 0)
150-
}
151-
152-
if (seekToPreviousCount > 0) {
153-
seekTo(rotateIndex(currentMediaItemIndex - seekToPreviousCount), 0)
154-
}
155-
156-
seekToNextCount = 0
157-
seekToPreviousCount = 0
158-
}
159-
}
145+
val seekCount = seekToNextCount - seekToPreviousCount
146+
if (seekCount != 0) {
147+
seekByCount(seekCount)
160148
}
161149
}
162150
}
163151

152+
private fun seekByCount(seekCount: Int) {
153+
runOnPlayerThread {
154+
if (currentMediaItem == null) {
155+
return@runOnPlayerThread
156+
}
157+
158+
val currentIndex = currentMediaItemIndex
159+
val seekIndex = if (shuffleModeEnabled) {
160+
val shuffledIndex = shuffledMediaItemsIndices.indexOf(currentIndex)
161+
val seekIndex = rotateIndex(shuffledIndex + seekCount)
162+
shuffledMediaItemsIndices.getOrNull(seekIndex) ?: return@runOnPlayerThread
163+
} else {
164+
rotateIndex(currentIndex + seekCount)
165+
}
166+
167+
seekTo(seekIndex, 0)
168+
seekToNextCount = 0
169+
seekToPreviousCount = 0
170+
}
171+
}
172+
164173
private fun rotateIndex(index: Int): Int {
165174
val count = mediaItemCount
166175
return (index % count + count) % count

0 commit comments

Comments
 (0)