1

I have to play a video in one of my screens of Android and I am using Video View intent for the same. The video gets played but there is no thumbnail appearing on the launch of the screen.

My code is like this

@OnClick(R.id.icon_play)
    protected void playVideo(){
        String videoUrl="https://someUrl/Video/v07.mp4";
        if(!videoUrl.isEmpty()) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
            intent.setDataAndType(Uri.parse(videoUrl), "video/mp4");
            startActivity(intent);
        }

    }

By thumbnail, I mean that when the screen is launched each video should have an image of its own. (something like YouTube)

I tried seekTo() for attaching the thumbnail, but it didn't work. Please Help. Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aayushi
  • 1,736
  • 1
  • 26
  • 48
  • this will help you https://stackoverflow.com/a/44943830/7666442 – AskNilesh Jan 03 '18 at 09:21
  • Have your tried using Bitmap and/or ThumbnailUtils? – Nero Jan 03 '18 at 09:21
  • No, I haven;t tried those @Nero – Aayushi Jan 03 '18 at 09:22
  • Possible duplicate of [How to create thumbnail of video url form server](https://stackoverflow.com/questions/44943575/how-to-create-thumbnail-of-video-url-form-server) – AskNilesh Jan 03 '18 at 09:28
  • for that you have to put a your VideoView together with ImageView inside FrameLayout with same dimension. after you have to hide the imageview when video start playing . for image you can use any method..i use as answer below.. – mitesh viradiya Jan 03 '18 at 09:33

3 Answers3

2

I solve the problem using MediaMetaDataRetriever.

The code goes like this-

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(-1,MediaMetadataRetriever.OPTION_CLOSEST);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new Throwable(
                    "Exception in retriveVideoFrameFromVideo(String videoPath)"
                            + e.getMessage());

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

Note that : Because my video link was in the form of server URL, that's why createThumnailUtils was returning a null when video Url was passed through it.

The below code works fine when the video is coming from local storage.

Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail("URL", MediaStore.Images.Thumbnails.MINI_KIND);

BitmapDrawable bitmapD = new BitmapDrawable(thumbnail);
VideoView.setBackground(Drawable bitmapD);

Hope this helps someone!!

Aayushi
  • 1,736
  • 1
  • 26
  • 48
0

Just looked at another example. Not 100% sure if it's going to work but worth a try.

Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail("URL", MediaStore.Images.Thumbnails.MINI_KIND);

BitmapDrawable bitmapD = new BitmapDrawable(thumbnail);
VideoView.setBackground(Drawable bitmapD);

Please note, I have written this over phone so there might be spelling errors. Let me know if this works or if you find another alternative

Nero
  • 1,058
  • 1
  • 9
  • 25
-1

this may be used i am using this method for thumbnail image of video on my list view of video player..

     Cursor cursor = mContext.getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
            MediaStore.Video.Media.DISPLAY_NAME + "=?",
            new String[]{localItem._display_name}, null);
    cursor.moveToFirst();
    long ids = cursor.getLong(cursor
            .getColumnIndex(MediaStore.Video.Media._ID));

    ContentResolver crThumb = mContext.getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(
            crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND,
            options);
    itemHolder.thumbImage.setImageBitmap(curThumb);
    curThumb = null;
    cursor.close();

try this way it may be helpful

above ans is now deprecated. new updated answer with fast one solution we just have to give the video data path

    Bitmap bmThumbnail;
    bmThumbnail = ThumbnailUtils.createVideoThumbnail(arraylist.get(i)._data, MediaStore.Video.Thumbnails.MICRO_KIND);
    itemHolder.thumbImage.setImageBitmap(bmThumbnail);

where arraylist.get(i)._data => path of video.

and best way is to use Glide or any other image lodging async library for smooth scrolling of listview.