一,通過Android系統自帶的類獲取:
1. public staticBitmapcreateVideoThumbnail(String filePath,int
kind){
Bitmap bitmap = null;
MediaMetadataRetriever retriever = newMediaMetadataRetriever();
try {
if (filePath.startsWith("http://")
|| filePath.startsWith("https://")
|| filePath.startsWith("widevine://")) {
retriever.setDataSource(filePath,newHashtable<String,String>());
}else {
retriever.setDataSource(filePath);
}
bitmap =retriever.getFrameAtTime(-1);
} catch (IllegalArgumentExceptionex) {
// Assume this is a corrupt video file
ex.printStackTrace();
} catch (RuntimeExceptionex) {
// Assume this is a corrupt video file.
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeExceptionex) {
// Ignore failures while cleaning up.
ex.printStackTrace();
}
}
if (bitmap==null)returnnull;
if (kind== MediaStore.Images.Thumbnails.MINI_KIND) {
// Scale down the bitmap if it's too large.
int width= bitmap.getWidth();
int height= bitmap.getHeight();
int max =Math.max(width, height);
if(max >512) {
float scale=512f
/ max;
int w =Math.round(scale * width);
int h =Math.round(scale * height);
bitmap = Bitmap.createScaledBitmap(bitmap,w, h,
true);
}
} else if (kind== MediaStore.Images.Thumbnails.MICRO_KIND) {
bitmap = ThumbnailUtils.extractThumbnail(bitmap,
96,
96,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
1. 根據視頻文件的路徑,我們可以通過android自帶的MediaMetadataRetriever類獲取到視頻文件的縮略圖,支持的視頻文件格式爲:mkv,mp4等。支持的視頻文件格式比較侷限,因此我選擇使用MediaMetadataRetriever的拓展類:FFmepgMediaMetadataRetriever來取更多格式的視頻文件縮略圖。
2. 下載prebuilt-aars.zip壓縮包,解壓後得到fmmr.aar文件(與jar文件類似),將fmmr.aar文件添加到項目中,即可創建FFmepgMediaMetadataRetriever對象,並獲取到視頻文件縮略圖
方案二:
//創建FFmpegMediaMetadataRetriever對象 FFmpegMediaMetadataRetriever mm=new FFmpegMediaMetadataRetriever();
try{ //獲取視頻文件數據 mm.setDataSource(path);
//獲取文件縮略圖 Bitmap bitmap=mm.getFrameAtTime(); }catch (Exception e){ } finally { mm.release(); }
參考:https://github.com/wseemann/FFmpegMediaMetadataRetriever/
參考:http://blog.csdn.net/zhiyahan/article/details/51793319
個人Demo:https://github.com/15008049860/FFmepg