MediaPlayer, ProgressBar

前端 未结 3 637
心在旅途
心在旅途 2020-12-03 08:18

Is this the correct way to update a ProgressBar when playing Media? I figured there would be a callback in MediaPlayer, but I couldn\'t find it.

mediaPla         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 09:16

    Take a look at android.widget.MediaController, it has a progress bar.

    They use a handler that recursively calls itself if appropriate. You can set the delay to however often you want the progress bar updated.

    Note that the controller can be shown or hidden as well as dragged by the user, and - of course - the video can stop. These are the reasons for the various checks (!mDragging && mShowing && mVideoView.isPlaying()) before another recursive call to update the bar.

    protected Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            int pos;
            switch (msg.what)
            {
                // ...
    
                case SHOW_PROGRESS:
                    pos = setProgress();
                    if (!mDragging && mShowing && mVideoView.isPlaying())
                    {
                        msg = obtainMessage(SHOW_PROGRESS);
                        sendMessageDelayed(msg, 1000 - (pos % 1000));
                    }
                    break;
    
                 // ...
            }
        }
    };
    

    To start it off use:

    mHandler.sendEmptyMessage(SHOW_PROGRESS);
    

    It will stop by itself, but you should cancel the last pending request using:

    mHandler.removeMessages(SHOW_PROGRESS);
    

提交回复
热议问题