Android加載視頻封面的兩種方式


 

package com.z.z.utils;

/*
 * :Created by z on 2020-08-31
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.VideoDecoder;
import com.bumptech.glide.request.RequestOptions;

import java.security.MessageDigest;
import java.util.HashMap;

public class VideoFrameTool {

    private static VideoFrameTool instance;

    public static VideoFrameTool getInstance() {
        if (instance == null) {
            instance = new VideoFrameTool();
        }
        return instance;
    }

    /**
     * 獲取網絡視頻第一幀
     *
     * @param videoUrl
     * @return
     */
    public void loadFirst(String videoUrl, @NonNull ImageView cover) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根據url獲取縮略圖
            retriever.setDataSource(videoUrl, new HashMap());
            //獲得第一幀圖片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            //e.printStackTrace();
            Log.e("zhu", e.toString());
        } finally {
            retriever.release();
        }
        if (bitmap != null) {
            cover.setImageBitmap(bitmap);
        }
    }

    /**
     *   context 上下文
     *   uri 視頻地址
     *   imageView 設置image
     *   frameTimeMicros 獲取某一時間幀
     */
    public void loadFirstWithGlide(final Context context, String uri, ImageView imageView, long frameTimeMicros) {
        RequestOptions requestOptions = RequestOptions.frameOf(frameTimeMicros);
        requestOptions.set(VideoDecoder.FRAME_OPTION, MediaMetadataRetriever.OPTION_CLOSEST);
        requestOptions.transform(new BitmapTransformation() {
            @Override
            protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
                return toTransform;
            }

            @Override
            public void updateDiskCacheKey(MessageDigest messageDigest) {
                try {
                    messageDigest.update((context.getPackageName() + "RotateTransform").getBytes("utf-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        Glide.with(context).load(uri).apply(requestOptions).into(imageView);
    }

    /**
     * 獲取本地視頻的第一幀
     *
     * @param localPath
     * @return
     */
    public Bitmap getLocalVideoBitmap(String localPath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根據文件路徑獲取縮略圖
            retriever.setDataSource(localPath);
            //獲得第一幀圖片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM