美文网首页
Android获取视频第一帧 first frame 的研究之路

Android获取视频第一帧 first frame 的研究之路

作者: adustdu2015 | 来源:发表于2018-10-09 17:12 被阅读0次

    现在大大小小的应用都包含视频,原来的做法就是想办法利用前台的一张图片作为封面,今天终于不用这个图片啦。
    研究路线:
    一。根据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

    结果

    相关文章

      网友评论

          本文标题:Android获取视频第一帧 first frame 的研究之路

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