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.
mediaPlayer.start();
final SeekBar progress = (SeekBar) dialog.findViewById(R.id.seekBar1);
progress.setMax(mediaPlayer.getDuration());
new CountDownTimer(mediaPlayer.getDuration(), 250) {
public void onTick(long millisUntilFinished) {
progress.setProgress(progress.getProgress() + 250);
}
public void onFinish() {}
}.start();
Best regards.
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.To start it off use:
It will stop by itself, but you should cancel the last pending request using: