Detect if a VideoVIew is buffering

17,737

Solution 1

Since API level 17, you can now access the InfoListener from the MediaPlayer:

final MediaPlayer.OnInfoListener onInfoToPlayStateListener = new MediaPlayer.OnInfoListener() {

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
            case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START: {
                mProgressBar.setVisibility(View.GONE);
                return true;
            }
            case MediaPlayer.MEDIA_INFO_BUFFERING_START: {
                mProgressBar.setVisibility(View.VISIBLE);
                return true;
            }
            case MediaPlayer.MEDIA_INFO_BUFFERING_END: {
                mProgressBar.setVisibility(View.GONE);
                return true;
            }
        }
        return false;
    }

});

mVideoView.setOnInfoListener(onInfoToPlayStateListener);

Solution 2

I came with the following hack in order to not implement a custom VideoView. The idea is to check every 1 second if the current position is the same as 1 second before. If it is, the video is buffering. If not, the video is really playing.

final Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 
    public void run() {
        int duration = videoView.getCurrentPosition();
        if (old_duration == duration && videoView.isPlaying()) {
            videoMessage.setVisibility(View.VISIBLE);
        } else {
            videoMessage.setVisibility(View.GONE);
        }
        old_duration = duration;

        handler.postDelayed(runnable, 1000);
    }
};
handler.postDelayed(runnable, 0);

videoMessage is just a TextView with the text "Buffering..." placed in the center of my VideoView.

Solution 3

Following code will show a buffering dialog every time the VideoView is buffering.

    final ProgressDialog bufferingDialog;
    bufferingDialog = ProgressDialog.show(context,
            "Buffering", "Please wait", true, true);

    VideoView videoView;
    videoView = (VideoView) findViewById(R.id.video_view);
    videoView.setVideoPath(path);
    videoView.setMediaController(new MediaController(context));
    videoView.requestFocus();
    videoView.start();
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
                        bufferingDialog.show();
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
                        bufferingDialog.dismiss();
                    return false;
                }
            });
        }
    });
    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            bufferingDialog.dismiss();
            return false;
        }
    });

Solution 4

VideoView showing Progress while Buffering.
Below code worked for me:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                mp.start();
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        switch (what) {
                            case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START: {
                                progressBar.setVisibility(View.GONE);
                                return true;
                            }
                            case MediaPlayer.MEDIA_INFO_BUFFERING_START: {
                                progressBar.setVisibility(View.VISIBLE);
                                return true;
                            }
                            case MediaPlayer.MEDIA_INFO_BUFFERING_END: {
                                progressBar.setVisibility(View.GONE);
                                return true;
                            }
                        }
                        return false;
                    }
                });
            }
        });

Solution 5

I'm working on something similar, and couldn't come up with a great solution. Some interesting solutions were posted here that you should check out if you haven't seen them.

Anyway, I came up with the following hack that was hinted at in the above thread and works ok for now.

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    float temp = ((float)mp.getCurrentPosition() / (float)mp.getDuration())*100;
    if(Math.abs(percent - temp) < 1) {
        buffer_fail++;
        if(buffer_fail == 15) {
            //buffer failed
        }
    }
}
Share:
17,737
tonyc
Author by

tonyc

Updated on June 21, 2022

Comments

  • tonyc
    tonyc almost 2 years

    Does anyone know if it's possible to detect when a VideoView is buffering?

    I want to show a ProgressDialog when the video is buffering.

    So far I tried using a OnPreparedListener, but that only works when the video is first loaded. If a video is playing and the user moves the scrub bar to a different point the video is still "prepared" even though it is buffering.

    I also tried (I know this is awful) an AsyncThread that just busy waits on isPlaying():

    private class BufferTask extends AsyncTask<Void, Void, Void> {
    
      protected Void doInBackground(Void...voids) {
        final VideoView videoView = (VideoView)findViewById(R.id.video);
        while (!videoView.isPlaying()) { }
        return null;
      }
    
     protected void onPostExecute(Void v) {
         // Hide the dialog here...
     }
    }
    

    This doesn't work because as soon as you call start() a VideoView seems to be considered playing even though it is buffering.

    The only solution I can think of is building a custom VideoView type class so I can access its MediaPlayer instance.

    Any ideas? Thanks for reading.

    • Pandiri Deepak
      Pandiri Deepak over 10 years
      Is your problem solved?
  • Rui
    Rui over 11 years
    This cannot be used inside a VideoView.
  • Behnam
    Behnam over 10 years
    This looks like a recursive exponential-memory-consumer piece of code. Isn't it?
  • Gil SH
    Gil SH almost 10 years
    So use it with the MediaPlayer that you get from VideoView.setOnPreparedListener
  • Saeed Neamati
    Saeed Neamati almost 7 years
    Shouldn't we hide progress on buffering end?
  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp almost 6 years
    Welcome to Stack Overflow! Please don't just throw your source code here. Try to give a good description to your answer. See: How do I write a good answer?