Search code examples
javaandroidandroid-videoviewandroid-video-playervideo-thumbnails

Generating thumbnail for videos in ImageView at a given position


The below code is working good, its generating image thumbnail for the video from the given url, but i want to know, is it possible to generate video thumbnail at suppose 100 milliseconds because by default its generating thumbnail at the beginning position so if in beginning the video screen is black then the thumbnail is also showing black so its looking quite odd. So is there any possibility to get the thumbnail by skipping some milliseconds.

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        String path;

        public DownloadImage(ImageView bmImage, String path) {
            this.bmImage = (ImageView ) bmImage;
            this.path=path;
        }

        protected Bitmap doInBackground(String... urls) {
            Bitmap myBitmap = null;
            MediaMetadataRetriever mMRetriever = null;
            try {
                mMRetriever = new MediaMetadataRetriever();
                if (Build.VERSION.SDK_INT >= 14)
                    mMRetriever.setDataSource(path, new HashMap<String, String>());
                else
                    mMRetriever.setDataSource(path);
                myBitmap = mMRetriever.getFrameAtTime();
            } catch (Exception e) {
                e.printStackTrace();


            } finally {
                if (mMRetriever != null) {
                    mMRetriever.release();
                }
            }
            return myBitmap;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

Solution

  • You can use other overloaded methods of MediaMetadataRetriever to get frame of a particular time.

    public Bitmap getFrameAtTime(long timeUs, int option) {
        throw new RuntimeException("Stub!");
    }
    
    public Bitmap getFrameAtTime(long timeUs) {
        throw new RuntimeException("Stub!");
    }
    
    public Bitmap getFrameAtTime() {
        throw new RuntimeException("Stub!");
    }