Android根據地址獲取視頻的第一幀

須要導入的三方框架:  compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'  

//線程
    private void setBitmap(final ImageView imageView, final String path) {
        //設置縮略圖的線程
        new Thread() {
            @Override
            public void run() {
                super.run();
                final Bitmap b = getVideoBitmap(path);
                if (b != null) {
                    Log.i("xxxxx", "得到到的縮略圖不爲空~");
                    imageView.post(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(b);
                        }
                    });
                } else {
                    Log.i("xxxxx", "得到到的縮略圖爲空~");
                }
            }
        }.start();
    }

    //BitMap獲取縮略圖
    private Bitmap getVideoBitmap(String path) {
        FFmpegMediaMetadataRetriever ffmmr = new FFmpegMediaMetadataRetriever();
        Bitmap bitmap = null;
        try {
            ffmmr.setDataSource(path);
            bitmap = ffmmr.getFrameAtTime();
            if (bitmap != null) {
                if (bitmap.getWidth() > 400) {// 若是圖片寬度規格超過640px,則進行壓縮
                    bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                            396, 260,
                            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                }
            }
        } catch (IllegalArgumentException ex) {
            Log.i("xxxxx", ex.toString());
        } finally {
            ffmmr.release();
        }
        return bitmap;
    }
}
相關文章
相關標籤/搜索