The first way:
public static Bitmap getVideoThumb(String path) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
//根据url获取缩略图
retriever.setDataSource(path, new HashMap());
//获得第一帧图片
bitmap = retriever.getFrameAtTime();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} finally {
retriever.release();
}
return bitmap;
}
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap bitmap = getVideoThumb(url)
imageView.setImageBitmap(bitmap);
videoPlayer.setThumbImageView(videoImage);
The second way:
/**
*
有个0- 到1秒的延迟
* @param context 上下文
* @param uri 视频地址
* @param imageView 设置image
* @param frameTimeMicros 获取某一时间帧
*/
public static ImageView loadVideoScreenshot(final Context context, String uri, ImageView imageView, long frameTimeMicros) {
RequestOptions requestOptions = RequestOptions.frameOf(frameTimeMicros);
requestOptions.placeholder(R.drawable.video);
requestOptions.set(VideoDecoder.FRAME_OPTION, MediaMetadataRetriever.OPTION_CLOSEST);
requestOptions.transform(new BitmapTransformation() {
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return toTransform;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
try {
messageDigest.update((context.getPackageName() + "RotateTransform").getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Glide.with(context).load(uri)
.apply(requestOptions).into(imageView);
return imageView;
}
1. An empty image will appear in the first
method, and the first frame image will appear when the loading is successful. 2. In the second method, using Glide, you can allocate a placeholder image during the loading process. And you can specify the Nth frame picture.