Skip to content

Commit

Permalink
fix Stackoverflow on next mediaitem is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
GrakovNe committed Dec 15, 2024
1 parent 6a42679 commit 208e2f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
applicationId = "org.grakovne.lissen"
minSdk = 28
targetSdk = 35
versionCode = 51
versionName = "1.1.20"
versionCode = 52
versionName = "1.1.21"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,37 @@ class PlaybackNotificationService @Inject constructor(
false -> Direction.BACKWARD
}

val nextTrack = findAvailableTrackIndex(exoPlayer.currentMediaItemIndex, direction, exoPlayer)
exoPlayer.seekTo(nextTrack, 0)
val nextTrack = findAvailableTrackIndex(exoPlayer.currentMediaItemIndex, direction, exoPlayer, 0)
nextTrack?.let { exoPlayer.seekTo(it, 0) }

if (nextTrack < currentIndex) {
if (nextTrack == null || nextTrack < currentIndex) {
exoPlayer.pause()
}
}
}
})
}

private fun findAvailableTrackIndex(currentItem: Int, direction: Direction, exoPlayer: ExoPlayer): Int {
private fun findAvailableTrackIndex(
currentItem: Int,
direction: Direction,
exoPlayer: ExoPlayer,
iteration: Int,
): Int? {
if (exoPlayer.getMediaItemAt(currentItem).mediaId != SilenceMediaSource::class.simpleName) {
return currentItem
}

if (iteration > 4096) {
return null
}

val foundItem = when (direction) {
Direction.FORWARD -> (currentItem + 1) % exoPlayer.mediaItemCount
Direction.BACKWARD -> if (currentItem - 1 < 0) exoPlayer.mediaItemCount - 1 else currentItem - 1
}

return findAvailableTrackIndex(foundItem, direction, exoPlayer)
return findAvailableTrackIndex(foundItem, direction, exoPlayer, iteration + 1)
}
}

Expand Down

0 comments on commit 208e2f1

Please sign in to comment.