简体   繁体   English

我如何从android中的远程url获取视频缩略图

[英]How can i get video thumbnail from remote url in android

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.我正在尝试从 URL 示例中获取缩略图:“https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4” 在 IOS 中使用 AVAssets 是可能的。 The way i am doing it is using the following function我这样做的方式是使用以下 function

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);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + 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.但问题是它会一次又一次地减慢回收视图和加载图像的速度。

I have solve the issue by using GLide 4.x.我已经使用 GLide 4.x 解决了这个问题。 I have find another solution by using MediaMetadataRetriever.我通过使用 MediaMetadataRetriever 找到了另一种解决方案。 But it's not feasible to use MediaMetadataRetriever in a recycle view because it runs on main thread and cause ANR.但在回收视图中使用 MediaMetadataRetriever 是不可行的,因为它在主线程上运行并导致 ANR。 The code that works for me is below.对我有用的代码如下。

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);

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM