您的位置:首页 > 理论基础 > 计算机网络

Android 获取视频(本地和网络)缩略图的解决方案

2016-06-30 20:49 405 查看
在Android 开发视频的时候,通常都需要显示视频列表,而视频列表通常都有一张视频缩略图,那么它是怎么获取的呢,

关于网络视频的缩略图的实现方案主要有两种:

1、后台返回视频时顺便连缩略图的路径都返回给你了,这样前端压力轻松。

2、后台是返回视频路径,关于缩略图,前端从视频中获取。

那么如何从视频中获取缩略图呢?

1、关于本地视频的缩略图,官方有提供解决方案:

ThumbnailUtils.createVideoThumbnail(String filePath, int kind);

方法便可以获取:

Bitmap bitmap= BitmapUtil.createVideoThumbnail(url,MediaStore.Video.Thumbnails.MINI_KIND);

2、那么网络视频怎么获取呢?

经过分析MediaMetadataRetriever类,发现有一个方法,setDataSource(String uri, Map

/**
* Sets the data source (URI) to use. Call this
* method before the rest of the methods in this class. This method may be
* time-consuming.
*
* @param uri The URI of the input media.
* @param headers the headers to be sent together with the request for the data
* @throws IllegalArgumentException If the URI is invalid.
*/
public void setDataSource(String uri,  Map<String, String> headers)
throws IllegalArgumentException {
int i = 0;
String[] keys = new String[headers.size()];
String[] values = new String[headers.size()];
for (Map.Entry<String, String> entry: headers.entrySet()) {
keys[i] = entry.getKey();
values[i] = entry.getValue();
++i;
}

_setDataSource(
MediaHTTPService.createHttpServiceBinderIfNecessary(uri),
uri,
keys,
values);
}


这个方法说明了是获取网络的数据,了解了源码就好办,我们就依葫芦画瓢,自己创建一个类模仿一个方法出来:

这个方法,调用了retriever.setDataSource(filePath,new Hashtable

/**
* Create a video thumbnail for a video. May return null if the video is
* corrupt or the format is not supported.
*
* @param filePath the path of video file
* @param kind could be MINI_KIND or MICRO_KIND
*/
public static Bitmap createVideoThumbnail(String filePath, int kind) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
if (filePath.startsWith("http://")
|| filePath.startsWith("https://")
|| filePath.startsWith("widevine://")) {
retriever.setDataSource(filePath,new Hashtable<String, String>());
}else {
retriever.setDataSource(filePath);
}
bitmap = retriever.getFrameAtTime(-1);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
ex.printStackTrace();
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
ex.printStackTrace();
}
}

if (bitmap == null) return null;

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;
}


这样就可以获取网络视频缩略图了,可能还存在版本兼容上没做兼容,欢迎提出意见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 视频