I want to create thumbnail from video url of server in android

无人久伴 提交于 2019-12-31 05:34:23

问题


My code,

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {

    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable(
                "Exception in retriveVideoFrameFromVideo(String videoPath)"
                        + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

This is Create thumbnail but take much time I used this with ListView then ListView being hangup.


回答1:


You need run this task in Async Method Like this in onBindViewHolder() if you are using RecycleView or put on getView() if your are using ListView:

 new AsyncTask<String, String, String>() {
            Bitmap bitmapVideo;

            @Override
            protected String doInBackground(String... strings) {
                try {
                   //Your method call here
                    bitmapVideo =retriveVideoFrameFromVideo(strings[0]);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String id) {
                super.onPostExecute(id);
                if (bitmapVideo != null) {
                  //Load your bitmap here
                    holder.imgVideoThumb.setImageBitmap(bitmapVideo);
                }
            }
        }.execute(getYourVideolink());

For better efficiency you save the bitmap image in local and before calling AsyncTask() check weather this image is already save in local if its their than load from local and no new to run AsyncTask() again



来源:https://stackoverflow.com/questions/42571465/i-want-to-create-thumbnail-from-video-url-of-server-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!