美文网首页android studio我爱编程
android获取视频指定帧方法总结

android获取视频指定帧方法总结

作者: 一蓑烟雨_8a41 | 来源:发表于2018-05-25 17:29 被阅读148次

1、通过MediaMetadataRetriever类获取指定帧,缺点,不支持m3u8

/*** 通过url获取视频开始第一帧画面,该方法不支持m3u8,支持mp4

    * @param url

    * @param type

    * @return

    */

    public static BitmapcreateVideoThumbnail(String url, int type) {

Bitmap bitmap =null;

        MediaMetadataRetriever retriever =new MediaMetadataRetriever();

        try {

//将网络文件以及本地文件区分开来设置

//            if (type == NETWORK) {

            retriever.setDataSource(url, new HashMap());

//            } else if(type == LOCAL){

//                retriever.setDataSource(url);

//            }

            bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST);

        }catch (IllegalArgumentException ex) {

//            LogUtils.e(ex.toString());

//            LogUtils.d("nihao", "获取视频缩略图失败");

        }finally {

try {

retriever.release();

            }catch (RuntimeException ex) {

//                LogUtils.e(ex.getMessage().toString());

//                LogUtils.d("nihao", "释放MediaMetadataRetriever资源失败");

            }

}

return bitmap;

    }

2 /**适用于5.0以上系统,获取的是屏幕截图

* 1:获取MediaProjectionManager

* 2:通过MediaProjectionManager.createScreenCaptureIntent()获取Intent

* 3:通过startActivityForResult传入Intent然后在onActivityResult中通过MediaProjectionManager.getMediaProjection(resultCode,data)获取MediaProjection

* 4:创建ImageReader,构建VirtualDisplay

* 5:最后就是通过ImageReader截图,就可以从ImageReader里获得Image对象。

* 6:将Image对象转换成bitmap

3 通过TextureView的getBitmap方法获取截图,获取的是视频的指定帧

4 Image image = videoCodec.getOutputImage(outputBufferIndex);

通过解码器,解码获取Image对象,转化为bitmap对象

相关文章

网友评论

  • 377f2d07fc64:那m3u8格式的怎么通过url这种方式获取缩略图啊

本文标题:android获取视频指定帧方法总结

本文链接:https://www.haomeiwen.com/subject/xreujftx.html