Skip to content

Instantly share code, notes, and snippets.

@dabluck
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabluck/36aa99f9d31eb2ec4684 to your computer and use it in GitHub Desktop.
Save dabluck/36aa99f9d31eb2ec4684 to your computer and use it in GitHub Desktop.
public class CustomVideoBitmapDecoder implements BitmapDecoder<ParcelFileDescriptor> {
private static final MediaMetadataRetrieverFactory DEFAULT_FACTORY = new MediaMetadataRetrieverFactory();
private static final int NO_FRAME = -1;
private MediaMetadataRetrieverFactory factory;
private int frame;
public CustomVideoBitmapDecoder() {
this(DEFAULT_FACTORY, NO_FRAME);
}
public CustomVideoBitmapDecoder(int frame) {
this(DEFAULT_FACTORY, checkValidFrame(frame));
}
CustomVideoBitmapDecoder(MediaMetadataRetrieverFactory factory) {
this(factory, NO_FRAME);
}
CustomVideoBitmapDecoder(MediaMetadataRetrieverFactory factory, int frame) {
this.factory = factory;
this.frame = frame;
}
@Override
public Bitmap decode(ParcelFileDescriptor resource, BitmapPool bitmapPool, int outWidth, int outHeight,
DecodeFormat decodeFormat)
throws IOException {
MediaMetadataRetriever mediaMetadataRetriever = factory.build();
mediaMetadataRetriever.setDataSource(resource.getFileDescriptor());
Bitmap result;
if (frame >= 0) {
result = mediaMetadataRetriever.getFrameAtTime(frame);
} else {
result = mediaMetadataRetriever.getFrameAtTime();
}
mediaMetadataRetriever.release();
resource.close();
return result;
}
@Override
public String getId() {
return "VideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap " + frame;
}
// Visible for testing.
static class MediaMetadataRetrieverFactory {
public MediaMetadataRetriever build() {
return new MediaMetadataRetriever();
}
}
private static int checkValidFrame(int frame) {
if (frame < 0) {
throw new IllegalArgumentException("Requested frame must be non-negative");
}
return frame;
}
}
BitmapPool bitmapPool = Glide.get(this).getBitmapPool();
FileDescriptorBitmapDecoder decoder = new FileDescriptorBitmapDecoder(
new VideoBitmapDecoder(200),
bitmapPool,
DecodeFormat.PREFER_ARGB_8888);
Glide.with(this).load(Environment.getExternalStorageDirectory().toString() +
"/source.mp4")
.asBitmap()
.videoDecoder(decoder)
.into(imageView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment