Search code examples
androidexoplayerexoplayer2.xandroid-video-player

How to programatically seek to a particular position/progress in DefaultTimeBar of Exoplayer


Exo player has a widget that handles the progress called DefaultTimeBar

<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@+id/exo_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Using this how-to programmatically seek to a particular position like 50% in android


Solution

  • Through inspecting the source code, it seems like DefaultTimeBarcalls update() constantly to retrieve bufferedPositionand position and draw the view accordingly. But it also has a couple of functions that call update() themselves and redraw everything. It is first initialized under the hood with a context and a few other parameters like AttributeSet.

    From here, it really depends on what you're willing to do with it. If you want to just seek the position in ExoPlayer (which I assume you already know), you can just call this

        val desiredPosition = simpleExoPlayer.duration / 2.toLong() // 50%
        simpleExoPlayer.seekTo(desiredPosition)
    

    However, if you're miscellaneously using the view as a Custom View and wanna update your DefaultTimeBar on your own. You need to understand how to instantiate it. Our DefaultTimeBar will receive position and bufferedPosition value updates constantly from the class that instantiated it to begin with; PlayerControlView.

    Let's break it down quickly, how a DefaultTimeBar is instantiated and updated:
    1- Instantiation:

        val timeBar: DefaultTimeBar = DefaultTimeBar(context, null, 0, playbackAttrs)
    

    As you can see, it takes a context, a @Nullable AttributeSet, a defStyleAttr and a @Nullable timeBar AttributeSet. Of course this can be a little bit confusing at first, but for starters, try going with the sample line of code above to instantiate it. The playbackAttrs in the constructor are the same playbackAttrs passed from SimpleExoPlayer to PlayerControlView in order to instantiate it. If playbackAttrs are still a cause of confusion, please inspect the source code of SimpleExoPlayer.

    2- Updating positions: After you have instantiated your DefaultTimeBar, you can constantly call these two functions on the instance you have

          timeBar.setPosition(position)
          timeBar.setBufferedPosition(bufferedPosition)
    

    3- Modifying a DefaultTimeBar view that is inflated from the XML: I have no particular experience howsoever with manipulating the DefaultTimeBar, but I assume you can just pass the generated view as a DefaultTimeBar and store it in a variable, that is:

        val timeBar: DefaultTimeBar = findViewById(R.id.defaulttimebar) as DefaultTimeBar
    

    This MAY or MAY NOT work. DefaultTimeBar extends "View" so the casting may or may not work.

    If you wanna seek to a particular position, always make sure you know the target position (For example, a result of a user-induced seeking), then call setPosition to update the DefaultTimeBar.