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(安卓)获取当前语言的方法1
- Android应用程序获取ROOT权限的方法(android中如何通过代码检测
- android 银联支付接入报nullexception异常
- android 获取wifi 信号质量
- android获取mac地址
- android intent 传递对象需要序列化实现Parcelable接口
- android 获取网络IP地址
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程