Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if VideoView is playing video or Buffering?

How to detect if VideoView is playing video or Buffering?
I want to display a pop-up saying video is buffering.

In android API level 17 there is a call back setOnInfoListener that can provide me this information but i am using API level 15 (android ICS).

I have also seen this question "Detect if a VideoVIew is buffering" but the suggested solution is for MediaPlayer and not for VideoView.

SO how can I detect if VideoView is buffering? is it a good solution to run a thread to check the current seek/progress level and depending on that decide if video is playing or buffering.

UPDATE

It is not like i just need to check if video is playing or buffering at the start of the video only, i want to check it through out video paying.

like image 962
User7723337 Avatar asked Apr 11 '13 06:04

User7723337


2 Answers

To check if VideoView is playing is not you can use its isPlaying() method,

if ( videoView.isPlaying() )
{
     // Video is playing
}
else
{
     // Video is either stopped or buffering
}

To check if VideoView is completed use following,

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
{
    @Override
    public void onCompletion(MediaPlayer mp) 
    {
             // Video Playing is completed
    }
});
like image 175
Lucifer Avatar answered Sep 30 '22 00:09

Lucifer


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);
like image 32
Attif Avatar answered Sep 29 '22 22:09

Attif