Improve behavior when seeking to next video

This commit is contained in:
Bnyro 2023-01-21 18:44:45 +01:00
parent f2fce67454
commit c3b08ef5f6

View File

@ -179,25 +179,24 @@ class AudioPlayerFragment : BaseFragment() {
* Update the position, duration and text views belonging to the seek bar * Update the position, duration and text views belonging to the seek bar
*/ */
private fun updateSeekBar() { private fun updateSeekBar() {
val duration = playerService?.getDuration()?.toFloat() ?: return val duration = playerService?.getDuration()?.takeIf { it > 0 } ?: let {
// if there's no duration available, clear everything
// when the video is not loaded yet, retry in 100 ms binding.timeBar.value = 0f
if (duration <= 0) { binding.duration.text = ""
binding.currentPosition.text = ""
handler.postDelayed(this::updateSeekBar, 100) handler.postDelayed(this::updateSeekBar, 100)
return return
} }
// get the current position from the player service
val currentPosition = playerService?.getCurrentPosition()?.toFloat() ?: 0f val currentPosition = playerService?.getCurrentPosition()?.toFloat() ?: 0f
// set the text for the indicators // set the text for the indicators
binding.duration.text = DateUtils.formatElapsedTime((duration / 1000).toLong()) binding.duration.text = DateUtils.formatElapsedTime(duration / 1000)
binding.currentPosition.text = DateUtils.formatElapsedTime( binding.currentPosition.text = DateUtils.formatElapsedTime(
(currentPosition / 1000).toLong() (currentPosition / 1000).toLong()
) )
// update the time bar current value and maximum value // update the time bar current value and maximum value
binding.timeBar.valueTo = duration / 1000 binding.timeBar.valueTo = (duration / 1000).toFloat()
binding.timeBar.value = minOf( binding.timeBar.value = minOf(
currentPosition / 1000, currentPosition / 1000,
binding.timeBar.valueTo binding.timeBar.valueTo