android获取视频的第一个画面,Android获取视频第一帧 first frame 的研究之路

现在大大小小的应用都包含视频,原来的做法就是想办法利用前台的一张图片作为封面,今天终于不用这个图片啦。

研究路线:

一。根据stackoverflow搜索结果,测试第一帧。(这个方法不行)

地址

If you use API level 8 and above. You can create preview of a video like this:

String videoFile = "/sdcard/blonde.mp4";

Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(videoFile,

MediaStore.Images.Thumbnails.MINI_KIND);

If you use API level 8 and above. You can create preview of a video like this:

String videoFile = "/sdcard/blonde.mp4";

Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(videoFile,

MediaStore.Images.Thumbnails.MINI_KIND);

Now you can show it in an ImageView:

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

imageView.setImageBitmap(thumbnail);

Or you can set it to a VideoView as a background, so that it is shown as a first video frame before the video starts playing:

VideoView video = (VideoView) findViewById(R.id.my_video_view);

BitmapDrawable bitmapDrawable = new BitmapDrawable(thumbnail);

video.setBackgroundDrawable(bitmapDrawable);

二.亲测可用(支持本地本件,也可以是网络地址的多媒体文件)

1.添加依赖

//获取第一帧的library

implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'

2.添加读取存储卡的权限,现在很多框架不多说啦

3.最关键代码:

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();

try {

// retriever.setDataSource("/storage/emulated/0/test.mp4"); //file's path

retriever.setDataSource("http://xyj.xueyijia.com.cn//2018/7/14/%E6%89%93%E9%A2%86%E5%B8%A6%E7%9A%84%E7%8B%90%E7%8B%B8%E5%B0%BC%E5%85%8B_2018_08_14_19_02_52.mp4"); //file's path

Bitmap bitmap = retriever.getFrameAtTime(100000,FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC ); //这个时间就是第一秒的

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

Glide.with(mContext).load(drawable).into(ivFrame);

} catch (Exception e) {

e.printStackTrace();

}

finally{

retriever.release();

}

4.over

cc80a958543a?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值