How to show seekbar with media player

Updated 26 October 2017

Save

What is a SeekBar? Well according to android.developers.com, A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. You must have seen a bar with your media player (audio or video). It helps you to navigate to a certain time of the audio or video you are using.

We are going to check that how we can achieve this in Android.

If you are not aware of the media player class then you can follow the below provided links to learn how to play the audios in your android app.

Playing Audio file online and offline

Let us begin with adding seek bar with your media player object.

We are gonna assume that you have you already have a Mediaplayer object initialized with your media. Now we will have to set the max value for the seek bar with the duration of the media file

and we need a handler which will constantly update the seek bar according to the media player object with the help of a Runnable. We are using this because only the main thread can update the UI.

the above handler updates the seek after every 50ms. You can change this time according to your requirement.

Now we have to call this handler when the audio starts to play. The below code segment shows how to do this.

and on pause, you can remove the callback.

 

The above process connects the seek bar to the media player but it will not seek the media player yet. For this, you will have to set an OnSeekBarChangeListener which will use the MediaPlayer’s seekTo function and seek the media file to a particular time.

this will seek to the position where ever the seek bar pointer is. The check fromUser is must otherwise it will keep seeking the media player on every second and the media output will come with some disturbance or lag.

That’s all you need to do to get a smoothly working SeekBar.

Thank you very much. This is Vedesh Kumar signing off.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


30 comments

  • Noor Eldali
    • Vedesh Kumar (Moderator)
    • Prashantgosai26
  • Bri
    • Vedesh Kumar (Moderator)
  • Arman
    • Vedesh Kumar (Moderator)
  • Saurabh
    • Vedesh Kumar (Moderator)
  • iniaski
    • Vedesh Kumar (Moderator)
  • Programmer
    • Vedesh Kumar (Moderator)
  • Prashant Kumar
    • Vedesh Kumar (Moderator)
  • reza motahari
    • Vedesh Kumar (Moderator)
  • mayar
    • Vedesh Kumar (Moderator)
  • Gyan Jyoti Das
    • Vedesh Kumar (Moderator)
      • Gyan Jyoti Das
        • Gyan Jyoti Das
          • Vedesh Kumar (Moderator)
        • Vedesh Kumar (Moderator)
  • Omar Adel
  • Aziz Fasih
    • anchit (Moderator)
  • Owais Ali
    • anchit (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home