首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android VideoView又回到了过去

Android VideoView又回到了过去
EN

Stack Overflow用户
提问于 2021-09-13 12:22:31
回答 2查看 98关注 0票数 2

我用自定义的搜索栏实现了videoView。当改变查找条位置时,视频播放正确,但时间显示错误。之所以会发生这种情况,是因为videoView的立场出于某种原因而返回。这种情况并不总是发生,而是经常发生。

SeekBar变更:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
                }
}

格式的持续时间:

代码语言:javascript
复制
@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()))
    )
}

日志:

代码语言:javascript
复制
#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: ----------------------------

在您可以看到,在寻找时间是257875,第一次定时器迭代也是257875。然后它跳到256957,我不明白为什么.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-22 18:40:31

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

在这种情况下,您可以从代码中筛选出进度更新,并且只有当fromUser在onProgressChanged是true时才能寻找新的视频位置:

代码语言:javascript
复制
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位置更新触发。

票数 1
EN

Stack Overflow用户

发布于 2021-09-21 19:23:11

我不确定,但我想问题在于postDelayed,因为它实际上在1秒后运行。因此,尝试在这里增加1000毫秒,希望它的行为正确。

代码语言:javascript
复制
RLog.d("Time display string: ${durationToFormat(binding.videoViewF.currentPosition + 1000)}")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69162620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档