Merge pull request #3521 from Bnyro/master

Update displayed chapter while scrubbing time bar
This commit is contained in:
Bnyro 2023-04-10 14:04:54 +02:00 committed by GitHub
commit a1c6b625f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -187,6 +187,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private val windowInsetsControllerCompat get() = WindowCompat
.getInsetsController(mainActivity.window, mainActivity.window.decorView)
private var scrubbingTimeBar = false
/**
* Receiver for all actions in the PiP mode
*/
@ -1200,14 +1202,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
// set the name of the video chapter in the exoPlayerView
private fun setCurrentChapterName() {
private fun setCurrentChapterName(position: Long? = null) {
// return if chapters are empty to avoid crashes
if (chapters.isEmpty() || _binding == null) return
// call the function again in 100ms
binding.player.postDelayed(this::setCurrentChapterName, 100)
val chapterIndex = getCurrentChapterIndex() ?: return
// if the user is scrubbing the time bar, don't update
if (scrubbingTimeBar && position == null) return
val chapterIndex = getCurrentChapterIndex(position) ?: return
val chapterName = chapters[chapterIndex].title.trim()
// change the chapter name textView text to the chapterName
@ -1222,8 +1227,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
/**
* Get the name of the currently played chapter
*/
private fun getCurrentChapterIndex(): Int? {
val currentPosition = exoPlayer.currentPosition / 1000
private fun getCurrentChapterIndex(position: Long? = null): Int? {
val currentPosition = (position ?: exoPlayer.currentPosition) / 1000
return chapters.indexOfLast { currentPosition >= it.start }.takeIf { it >= 0 }
}
@ -1523,7 +1528,15 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
SeekbarPreviewListener(
streams.previewFrames,
playerBinding,
streams.duration * 1000
streams.duration * 1000,
onScrub = {
setCurrentChapterName(it)
scrubbingTimeBar = true
},
onScrubEnd = {
scrubbingTimeBar = false
setCurrentChapterName(it)
}
)
)
}

View File

@ -17,7 +17,9 @@ import com.google.android.exoplayer2.ui.TimeBar
class SeekbarPreviewListener(
private val previewFrames: List<PreviewFrames>,
private val playerBinding: ExoStyledPlayerControlViewBinding,
private val duration: Long
private val duration: Long,
private val onScrub: (position: Long) -> Unit,
private val onScrubEnd: (position: Long) -> Unit
) : TimeBar.OnScrubListener {
private var moving = false
@ -35,6 +37,10 @@ class SeekbarPreviewListener(
playerBinding.seekbarPreviewPosition.text = DateUtils.formatElapsedTime(position / 1000)
processPreview(position)
runCatching {
onScrub.invoke(position)
}
}
/**