我需要将基于URL的视频缩略图显示到ImageView我的ListView项目的视图子项中,我找到了这个帖子但没有工作.
结果

码
thumb_image.setImageBitmap(new LoadVideoThumbnail().execute(URLs.videos +"/"+videos.get(position).getId()+".mp4").get());
Run Code Online (Sandbox Code Playgroud)
的AsyncTask
public class LoadVideoThumbnail extends AsyncTask<String, Object, Bitmap>{
@Override
protected Bitmap doInBackground(String... objectURL) {
//return ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND);
return ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND), 100, 100);
}
@Override
protected void onPostExecute(Bitmap result){
//img.setImageBitmap(result);
}
}
Run Code Online (Sandbox Code Playgroud)
Kis*_*oid 32
无需下载视频,您可以使用以下方法生成视频缩略图:
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 8
使用 Glide,我使用 Glide 4.8.0
long thumb = getLayoutPosition()*1000;
RequestOptions options = new RequestOptions().frame(thumb);
Glide.with(getBaseContext()).load(url).apply(options).into(mThumb);
Run Code Online (Sandbox Code Playgroud)
小智 -3
要显示视频的位图预览,只需设置VideoView的视频路径并让流缓冲即可。
例如:
videoView.setVideoPath("/sdcard/myawesomevideo.mp4");
Run Code Online (Sandbox Code Playgroud)
几秒钟后您将看到视频的第一帧。
| 归档时间: |
|
| 查看次数: |
26501 次 |
| 最近记录: |