Android获取视频的第一帧缩略图(本地视频、网络视频)

Android获取视频的第一帧缩略图(本地视频、网络视频)

1、利用 MediaMetadataRetrieve类

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        //获取网络视频
//        retriever.setDataSource(url, new HashMap<String, String>());
        //获取本地视频
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String videoName = simpleDateFormat.format(new Date()) + ".jpg";
        retriever.setDataSource(url);
        Bitmap bitmap = retriever.getFrameAtTime();
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(new File(context.getExternalCacheDir().getAbsolutePath()+ videoName + ".jpg"));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 10, outStream);
            tempPicUrl = SessionManager.getInstance().getAppFileDirPath() + videoName + ".jpg";
            tempVideoUrl = url;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        retriever.release();

其中:url是图片的地址
tempPicUrl是自定义的缩略图本地地址

public Bitmap getFrameAtTime(long timeUs, int option)//无参数时默认去第一帧
第一个参数是传入时间,只能是us(微秒)
第二个参数:
OPTION_CLOSEST 在给定的时间,检索最近一个帧,这个帧不一定是关键帧。
OPTION_CLOSEST_SYNC 在给定的时间,检索最近一个同步与数据源相关联的的帧(关键帧)。
OPTION_NEXT_SYNC 在给定时间之后检索一个同步与数据源相关联的关键帧。
OPTION_PREVIOUS_SYNC 在给定时间之前检索一个同步与数据源相关联的关键帧。

注:MediaMetadataRetrieve操作视频比较耗时,建议在子线程中进行操作

发布了43 篇原创文章 · 获赞 22 · 访问量 5928

猜你喜欢

转载自blog.csdn.net/qq_41466437/article/details/103265440
今日推荐