How to use a Seekbar in android as a seekBar as well as a progressBar simultaneously?

13,568

Finally I figured it out!!

The solution that I came up with was something like this :

First of all set the max progress for seekbar as the duration of the video

videoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            seekBar.setMax(videoView.getDuration());
            seekBar.postDelayed(onEverySecond, 1000);
        }
    });

This runnable will keep on updating the progressbar :

private Runnable onEverySecond=new Runnable() {

    @Override
    public void run() {

        if(seekBar != null) {
            seekBar.setProgress(videoView.getCurrentPosition());
        }

        if(videoView.isPlaying()) {
            seekBar.postDelayed(onEverySecond, 1000);
        }

    }
};

And then the setOnSeekBarChangeListener for the seekbar can be used to take the user seeks on the media. By using the fromUser boolean we can make out whether it was a call back due to user's interaction or the

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {

            if(fromUser) {
                // this is when actually seekbar has been seeked to a new position
                videoView.seekTo(progress);
            }
        }
    });
Share:
13,568
Prasad Korhale
Author by

Prasad Korhale

Updated on July 17, 2022

Comments

  • Prasad Korhale
    Prasad Korhale almost 2 years

    I intend to write a custom media controller for my app. I was planning to use seekbar to do both seeking and showing progress. Trying to implement this.

    Does anyone have such implementation or am I on the wrong path ?

  • Prasad Korhale
    Prasad Korhale over 12 years
    Thanks for the reply. But I am using secondary progress to show the streamed amount. I am posting below what I have come up with.
  • java acm
    java acm about 7 years
    great ! but when video go to end and i want to play videoView.start() it again seekBar not working , how to resolve this problem ?