How to Load Video Thumbnail From Video Url in ListView adapter

雨燕双飞 提交于 2019-12-08 13:28:48

问题


I have a ListView and I want to populate it with video form a URL. I can show Pictures in ListView adapter easily from URL, but how to load a video thumbnail?


回答1:


You can use MediaMetadataRetriever to get your video thumbnail.

new AsyncTask<Void, Void, Bitmap>() {

    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bitmap = null;
        String videoPath = "http://techslides.com/demos/sample-videos/small.mp4";
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 14)
                // no headers included
                mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
            else
                mediaMetadataRetriever.setDataSource(videoPath);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime();
        } catch (Exception e) {
            e.printStackTrace();

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

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap != null)
            ((ImageView) findViewById(R.id.your_image_id)).setImageBitmap(bitmap);
    }
}.execute();


来源:https://stackoverflow.com/questions/41956167/how-to-load-video-thumbnail-from-video-url-in-listview-adapter

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