Last active
August 29, 2015 14:22
-
-
Save dabluck/36aa99f9d31eb2ec4684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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