项目中有一个需求,将本地视频中的图片作为封面显示在播放器上方,查阅部分资料发现其实方法很简单,如下:
//videoUrl: 示例 /storage/sdcard/.../xx.mp4
private Bitmap getVideoBit(String videoUrl){
Bitmap bitmap = null;
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
File file = new File(videoUrl);
if(file.exists()){
//设置数据源为该文件对象指定的绝对路径
metadataRetriever.setDataSource(file.getAbsolutePath());
//获得视频第一帧的Bitmap对象
bitmap = metadataRetriever.getFrameAtTime();
}
return bitmap;
}
通过使用MediaMetadataRetriever类,可以方便地从本地视频文件中提取第一帧作为封面图片。只需设置数据源为视频文件的绝对路径,然后调用getFrameAtTime()方法即可获取Bitmap对象。

3818

被折叠的 条评论
为什么被折叠?



