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

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对象

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容