0

I wanna connect my media player to the progress bar Here is my code :

var media55 :MediaPlayer
var progressBar55 :ProgressBar

        progressBar55=findViewById(R.id.progressBar)
        media55 = MediaPlayer()
        media55.setDataSource("SONG URL HERE")
        media55.prepare()

I wanna when I start media player the progress bar progress with the media player

MKS
  • 15
  • 5

2 Answers2

0

Define Handler in class scope.

private val handler = Handler()

Then set your ProgressBar max value to duration of the MediaPlayer data source.

progressBar55.max = media55.duration

Finally add a Runnable object to set progress on your ProgressBar every X milliseconds.

val interval: Long = 1000
val statusChecker = object : Runnable {
    override fun run() {
        progressBar.progress = media.currentPosition
        handler.postDelayed(this, interval)
    }
}

When you run your audio you should call statusChecker.run() to start updating porgress, and when you are done remove Handler callbacks by calling handler.removeCallbacksAndMessages(null)

Rinri
  • 16
  • 2
0

I hope Its Hepls You

Using Handler

private lateinit var mHandler: Handler
private lateinit var mRunnable:Runnable

Just Create Function For This

fun startedSong()
{
mRunnable = Runnable {
                // Do something here
    if(media55.isPlaying)
    {
       progressBar55.progress=media55.currentPosition
    }
}
                // Schedule the task to repeat after 1 second
                mHandler.postDelayed(
                        mRunnable, // Runnable
                        1000 // Delay in milliseconds
                )
}

Call This Function In Your Code

    progressBar55=findViewById(R.id.progressBar)
    media55 = MediaPlayer()
    media55.setDataSource("SONG URL HERE")
    media55.prepare()
    progressBar55.max=media55.duration
    mHandler = Handler()
    startedSong()
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65