Handle shuffle when lazily seeking to next/previous media items

This commit is contained in:
Naveen 2023-10-17 07:11:24 +05:30
parent 4399b8518e
commit 316bb438b3
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3

View file

@ -142,25 +142,34 @@ class SimpleMusicPlayer(private val exoPlayer: ExoPlayer) : ForwardingPlayer(exo
seekJob?.cancel()
seekJob = scope.launch {
delay(timeMillis = 400)
if (seekToNextCount > 0 || seekToPreviousCount > 0) {
runOnPlayerThread {
if (currentMediaItem != null) {
if (seekToNextCount > 0) {
seekTo(rotateIndex(currentMediaItemIndex + seekToNextCount), 0)
}
if (seekToPreviousCount > 0) {
seekTo(rotateIndex(currentMediaItemIndex - seekToPreviousCount), 0)
}
seekToNextCount = 0
seekToPreviousCount = 0
}
}
val seekCount = seekToNextCount - seekToPreviousCount
if (seekCount != 0) {
seekByCount(seekCount)
}
}
}
private fun seekByCount(seekCount: Int) {
runOnPlayerThread {
if (currentMediaItem == null) {
return@runOnPlayerThread
}
val currentIndex = currentMediaItemIndex
val seekIndex = if (shuffleModeEnabled) {
val shuffledIndex = shuffledMediaItemsIndices.indexOf(currentIndex)
val seekIndex = rotateIndex(shuffledIndex + seekCount)
shuffledMediaItemsIndices.getOrNull(seekIndex) ?: return@runOnPlayerThread
} else {
rotateIndex(currentIndex + seekCount)
}
seekTo(seekIndex, 0)
seekToNextCount = 0
seekToPreviousCount = 0
}
}
private fun rotateIndex(index: Int): Int {
val count = mediaItemCount
return (index % count + count) % count