如何在安卓中从远程 URL 获取视频缩略图

huangapple go评论72阅读模式
英文:

How can i get video thumbnail from remote url in android

问题

我正在尝试从URL获取缩略图,例如:"https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"。在iOS中可以使用AVAssets来实现。我正在使用以下函数来进行操作:

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable("在 retriveVideoFrameFromVideo(String videoPath) 中出现异常:" + e.getMessage());
    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

但问题是,它会拖慢回收视图(recycleview)并且不断加载图像。

英文:

I am trying to get thumbnail from a URL Example: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" It is possible in IOS using AVAssets. The way i am doing it is using the following function

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
        Bitmap bitmap = null;
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT &gt;= 14)
                mediaMetadataRetriever.setDataSource(videoPath, new HashMap&lt;String, String&gt;());
            else
                mediaMetadataRetriever.setDataSource(videoPath);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Throwable(&quot;Exception in retriveVideoFrameFromVideo(String videoPath)&quot; + e.getMessage());
        } finally {
            if (mediaMetadataRetriever != null) {
                mediaMetadataRetriever.release();
            }
        }
        return bitmap;
    }

But the issue is it's slow down the recycleview and load image again and again.

答案1

得分: 5

我已经通过使用GLide 4.x解决了这个问题。我已经通过使用MediaMetadataRetriever找到了另一个解决方案。但在RecyclerView中使用MediaMetadataRetriever是不可行的,因为它在主线程上运行,会导致ANR(应用无响应)。适用于我情况的代码如下:

RequestOptions requestOptions = new RequestOptions();
Glide.with(context)
    .load("Your URL")
    .apply(requestOptions)
    .thumbnail(Glide.with(context).load("Your URL"))
    .into(holder.img_video_attachment_preview);
英文:

I have solve the issue by using GLide 4.x. I have find another solution by using MediaMetadataRetriever. But it's not feasible to use MediaMetadataRetriever in a recycle view because it runs on main thread and cause ANR. The code that works for me is below.

RequestOptions requestOptions = new RequestOptions();
                Glide.with(context)
                        .load(&quot;Your URL&quot;)
                        .apply(requestOptions)
                        .thumbnail(Glide.with(context).load(&quot;Your URL&quot;))
                        .into(holder.img_video_attachment_preview);

答案2

得分: 0

你的答案对我很有用,稍微调整了一下,我最终得到了一个稍微更短的版本。虽然你可能有自己这样做的原因...

GlideApp.with(context)
    .load(item.videoURL) // 你的视频链接
    .into(image_view)
英文:

Your own answer worked for me, and tweaking it a little bit, I ended up with a slightly shorter version. Although you might have had your own reasons for doing it that way...

GlideApp.with(context)
    .load(item.videoURL) // your video url
    .into(image_view)

huangapple
  • 本文由 发表于 2020年9月22日 21:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64011079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定