【问题标题】:Android VideoView jumps back in timeAndroid VideoView 及时跳转
【发布时间】:2023-03-10 07:38:02
【问题描述】:

我已经使用自定义搜索栏实现了videoView。更改搜索栏位置时,视频播放正确,但时间显示错误。发生这种情况是因为videoView 位置由于某种原因而退回。它并不总是发生,而是经常发生。

SeekBar 变化:

binding.progressBarVideo.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {


            if (underSeek) {
                binding.textViewTime.text = durationToFormat(binding.videoViewF.currentPosition)
                binding.videoViewF.seekTo(seekBar.progress)

                RLog.d("------------------")
                RLog.d("Seekbar progress: ${seekBar.progress}")
                RLog.d("videoView position: ${binding.videoViewF.currentPosition}")
                RLog.d("Time display string: ${durationToFormat(binding.videoViewF.currentPosition)}")
            }
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {
            mainHandler.removeCallbacks(updateTextTask)
            underSeek = true
        }

        override fun onStopTrackingTouch(seekBar: SeekBar?) {
            mainHandler.post(updateTextTask)
            underSeek = false
            hideTimer = 0
        }

    })

updateTextTask:

private val updateTextTask = object : Runnable {
    override fun run() {

        RLog.d("----------------------------")
        RLog.d("videoView position: ${binding.videoViewF.currentPosition}")
        RLog.d("Time display string: ${durationToFormat(binding.videoViewF.currentPosition)}")

        binding.textViewTime.text = durationToFormat(binding.videoViewF.currentPosition)
        binding.progressBarVideo.progress = binding.videoViewF.currentPosition
        mainHandler.postDelayed(this, 1000)
                }
}

格式化持续时间:

@SuppressLint("DefaultLocale")
private fun durationToFormat(duration: Int): String {

    return java.lang.String.format(
        "%02d:%02d",
        TimeUnit.MILLISECONDS.toMinutes(duration.toLong()),
        TimeUnit.MILLISECONDS.toSeconds(duration.toLong()) -
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration.toLong()))
    )
}

日志:

#onProgressChanged: ------------------
#onProgressChanged: Seekbar progress: 257875
#onProgressChanged: videoView position: 257875
#onProgressChanged: Time display string: 04:17
#run: ----------------------------
#run: videoView position: 257875
#run: Time display string: 04:17
#run: ----------------------------
#run: videoView position: 256957
#run: Time display string: 04:16
#run: ----------------------------

你可以看到,seek时间是257875后,第一次定时器迭代也是257875,然后跳到256957,我不明白为什么......

【问题讨论】:

    标签: android kotlin android-videoview


    【解决方案1】:

    您似乎再次从 runnable 更新进度,因此将从循环中调用 onProgressChanged 并将进度重置到以前的位置。

    在这种情况下,您可以从代码中过滤掉进度更新,并仅在 onProgressChanged 时 fromUsertrue 时寻找新的视频位置:

    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        if (fromUser) {
            binding.videoViewF.seekTo(seekBar.progress)
        }
        binding.textViewTime.text = durationToFormat(binding.videoViewF.currentPosition)
    }
    
    private val updateTextTask = object : Runnable {
        override fun run() {
            binding.progressBarVideo.progress = binding.videoViewF.currentPosition
            mainHandler.postDelayed(this, 1000)
        }
    }
    

    同样在这种情况下,您可以从 runnable 中删除文本更新,因为它将由 ProgressBar 位置更新触发。

    【讨论】:

      【解决方案2】:

      我不确定,但我想问题出在 postDelayed,因为它实际上在 1 秒后运行。所以尝试在这里添加 1000 毫秒,希望它可以正常运行。

      RLog.d("Time display string: ${durationToFormat(binding.videoViewF.currentPosition + 1000)}")
      

      【讨论】:

        猜你喜欢
        • 2012-01-25
        • 2012-06-22
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-26
        • 2012-04-03
        • 1970-01-01
        相关资源
        最近更新 更多