MooNChilD Song
MooNChilD Song

Reputation: 265

How to make the thumb of seekbar follows the current time of Videoview?

I somehow made my videoview to follow the seekbar thumb, but I could not find the way how to do vice versa: to make thumb follows current time of video view.

Here is my code.

class play_video : AppCompatActivity() {
    var seekbar:SeekBar? = null
    var vidView:VideoView? = null
    var current_time:TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_play_video)

        seekbar = findViewById<SeekBar>(R.id.seekbar_movie)
        vidView = findViewById<VideoView>(R.id.videoView)
        current_time = findViewById<TextView>(R.id.currenttime)


        val viduri = Uri.parse("android.resource://" + packageName + "/" + R.raw.test1)
        val updateHandler = Handler()
        vidView?.setVideoURI(viduri)
        val metadata = MediaMetadataRetriever()
        metadata.setDataSource(this,viduri)
        val vidLength = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
        seekbar?.max = vidLength!!.toInt()
        set_time(vidLength!!.toInt())

        vidView?.setOnClickListener {
            if(vidView!!.isPlaying)
                vidView?.pause()
            else
                vidView?.start()
        }
        seekbar?.setProgress(vidView!!.currentPosition)

        vidView?.start()

        seekbar?.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener
        {
            override fun onProgressChanged(p0: SeekBar, p1: Int, p2: Boolean)
            {
                seekbar?.progress?.let { vidView?.seekTo(it) }
                val p1_sec = (p1/1000).toInt()
                val p1_minute = (p1_sec/60).toInt()
                val p1_second = p1_sec%60
                current_time?.text = p1_minute.toString() + ":" + p1_second.toString()
            }
            override fun onStartTrackingTouch(p0: SeekBar)
            {
                seekbar?.progress?.let { vidView?.seekTo(it) }
            }
            override fun onStopTrackingTouch(p0: SeekBar)
            {
                if(vidView!!.isPlaying)
                    vidView?.start()
            }
        })
    }

I have searched many articles but could not understand how to do with videoview solely. Should I Re-code whole using MediaPlayer or is there any better way ?

Upvotes: 1

Views: 994

Answers (1)

Pascal
Pascal

Reputation: 123

You just create a handler, that repeats and sets the seekbar. Put this code into the onCreate function.

val handler = Handler(Looper.getMainLooper())
runOnUiThread(object : Runnable {
    override fun run() {
        seekbar?.setProgress(vidView!!.currentPosition)
        handler.postDelayed(this, 1000) //Set the update time
    }
}

The 1000 milliseconds is just a example, if you want to have your seekbar update faster you should use a smaller delay.

I did it myself, it is the best compromise between a live seekbar and the performance. You can also edit the delay for a faster update.

But keep in mind to add a check into the onProgressChanged like if(p2) { } so that the progress only changes if the user changes the progress and not the system itself. Like that:

override fun onProgressChanged(p0:SeekBar, p1:Int, p2:Boolean)
            {
                if(p2) {
                    seekbar?.progress?.let { vidView?.seekTo(it) }
                    val p1_sec = (p1/1000).toInt()
                    val p1_minute = (p1_sec/60).toInt()
                    val p1_second = p1_sec%60
                    current_time?.text = p1_minute.toString()+":"+p1_second.toString()
               }
            }

Upvotes: 3

Related Questions