Android获取网络视频文件缩略图
一,通过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
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
更多相关文章
- android intent 传递对象需要序列化实现Parcelable接口
- Android MIME类型与文件后缀名匹配部分
- android 文件存储
- android 文字或者图片生成.pdf文件
- android文件解析InputStream问题解决
- Android学习 数据存储之_文件存储
- Android Mediacodec H.265文件播放
- android 获取音乐文件的内置专辑图片
- Android 文件存储及常见问题解决