0

I am trying to popup a snack bar during a random time when the video plays I already got the video duration plus able to run the video. I have also able to set up the handler to show the snack bar at random time during the video duration.

Uri uri2 = Uri.parse("my video link");

MediaPlayer mp = MediaPlayer.create(this, uri2);
int duration = mp.getDuration();
mp.reset();
mp.release();

mVideoView2.setVideoURI(uri2);
mVideoView2.requestFocus();
mVideoView2.start();

I have only one problem. When I call videoview.start() it doesn't start the video immediately, it has some buffer time. I only want to show up the snackbar at random time during the video play. Assume the video length is 7 sec and my random time is 3 sec, and buffer time or preloading time is like 3 sec, the snackbar shows up before my video. Is there some way to know the buffer time or when the video is ready so that I can start calling my handler.

Chit Khine
  • 830
  • 1
  • 13
  • 34
  • I would suggest to check the current position for the player ```getCurrentPosition``` every second with a handler, then if the ```getCurrentPosition``` returns a value equal with your random time show the toast. Note ```getCurrentPosition``` returns time in miliseconds. – danypata Mar 07 '17 at 10:24

2 Answers2

0

Set a OnPreparedListener to get notified when video loads

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //play video... video is ready to play
        }
    });
Richard K Maleho
  • 436
  • 6
  • 16
  • 1
    just call `mVideoView2.start();` in `onPrepared` method – Richard K Maleho Mar 07 '17 at 10:20
  • That won't work if there is a bad internet connection. It's possible that the player to buffer one more time or several times due to connection. In that case, there's a problem. – danypata Mar 07 '17 at 10:22
  • `onPrepared` Is called when the media file is ready for playback, so it wont matter if there is a bad internet connection coz the media file will be already buffered – Richard K Maleho Mar 07 '17 at 10:31
  • @RichardKMaleho actually, it working the same way as I called videoView.start(), thanks for the help. – Chit Khine Mar 07 '17 at 10:58
0

Check: https://developer.android.com/reference/android/media/MediaPlayer.OnInfoListener.html, mainly MEDIA_INFO_VIDEO_RENDERING_START

Ionut Popa
  • 61
  • 3