英文:
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 >= 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.
答案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("Your URL")
.apply(requestOptions)
.thumbnail(Glide.with(context).load("Your URL"))
.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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论