How to detect if VideoView is playing video or Buffering?

五迷三道 提交于 2019-11-30 02:56:26

问题


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.


回答1:


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
    }
});



回答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);



回答3:


           videoView.setOnPreparedListener(new OnPreparedListener()
           {

               public void onPrepared(MediaPlayer mp)
               {                  
                   progressDialog.dismiss();     // or hide any popup or what ever
                   videoView.start();           // start the video
               }
           });  


来源:https://stackoverflow.com/questions/15941976/how-to-detect-if-videoview-is-playing-video-or-buffering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!