MediaMetadataRetriever类提供了一个统一的接口用于从一个输入媒体文件中取得帧和元数据。
获取视频缩略图
private static Bitmap createVideoThumbnailBitmap(String filePath, FileDescriptor fd,
int targetWidth) {Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
if (filePath != null) {
retriever.setDataSource(filePath);
} else {
retriever.setDataSource(fd);
}
bitmap = retriever.getFrameAtTime(-1);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
if (bitmap == null) return null;
// Scale down the bitmap if it is bigger than we need.
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width > targetWidth) {
float scale = (float) targetWidth / width;
int w = Math.round(scale * width);
int h = Math.round(scale * height);
bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
}
return bitmap;
}