每秒显示视频的不同帧

问题描述

在这个回收者视图中填充了文件信息。这些文件是视频,所以我会显示缩略图及其名称

我想要做的是能够显示视频的 3 帧,每秒都在变化。

例如,如果此视频时长为 20 秒,我将显示第 6 秒、第 12 秒和第 18 秒的帧。

嗯,我尝试过这样做,结果非常糟糕。对于每个视频,我必须获得 3 帧,这很乏味,而且非常慢,而且当您尝试滚动时,明显的回收器视图会滞后,因为这是有效的。

我会把代码在这里

private fun setVideoThumbnailFrames(file: FileItem,imageView: ImageView,timer: Timer) {
   coroutinescope.launch {
       val bitmapFrames = getThreeFrames(file)
        timer.schedule(object : TimerTask() {
            var i = 0
            override fun run() {
                Log.i("SeeThumbnail","Change $i")
                if (i == 2) {
                    i = 0
                }
                i += 1
                Handler(Looper.getMainLooper()).post {
                    Glide.with(imageView.context).asBitmap().load(bitmapFrames[i])
                        .into(BitmapImageViewTarget(imageView))
                    }
                }
            },1000,1000)
        }
    }
private fun getThreeFrames(file: FileItem): List<Bitmap> {
    val bitmapFrames = mutablelistof<Bitmap>()
    for (i in 1..3) {
        val bitmapFrame = MediaMetadataRetrieverUtils.getFrameTime(file,i)
        bitmapFrames.add(bitmapFrame)
    }
    return bitmapFrames
}
fun getFrameTimeInSeconds(file: File,times: Int): Bitmap {
    val durationInSeconds = getFileDurationInSeconds(file).toLong()/3
    mediaMetadataRetriever.setDataSource(file.absolutePath)
    return mediaMetadataRetriever.getFrameAtTime((durationInSeconds*times)*1000)!! //multiply by 1000 to make it milliseconds
}
fun getFileDurationInSeconds(file: File): Int {
    mediaMetadataRetriever.setDataSource(file.absolutePath)
    val durationInMillisMedia =
       mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_DURATION)

    return if (durationInMillisMedia == null) {
        0
    } else {
        durationInMillisMedia.toInt()/1000 // divide by 1000 to get seconds
    }
}

问题:有什么方法可以让我不知道将帧作为视频的缩略图 gif 并将其设置在视图上吗?

或者有没有办法解决滚动回收器时的滞后问题,以便在尝试获取视频帧时不慢?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)