How to add Listener for next , previous , rewind and forward in Exoplayer

Viewed 13654

I am working on ExoPlayer, I want to customize the ExoPlayer and listen for the Event next, previous, rewind, forward so that when the user clicks the next button next video in the Playlist will be playing and when using previous the previous video in the playlist will be playing and so on. I am using the Custom Layout, it changes the design but not listen for these events(next, previous etc). here is my code:-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:resize_mode="fill"
        app:controller_layout_id="@layout/custom_controls"
        app:player_layout_id="@layout/custom_player_view"
        app:surface_type="texture_view"/>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"/>
</RelativeLayout>




<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_marginTop="220dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

I have added the list of Mediasource in ConcatenatingMediaSource. It is working but I am to change the layout also, that why I need a listener for every control in ExoPlayer. I see this link. So I know onPositionDiscontinuity() method calls every time when the user clicks on next, previous, rewind, forward button in ExoPlayer But I want listener for each button so I can perform the appropriate action??

thanks

6 Answers
 if you have not customized exoplayer



    if (currentPlayingVodPosition-1 == player.currentWindowIndex){
 Toast.makeText(this,"prev clicked",Toast.LENGTH_SHORT).show()

                    }
if (currentPlayingVodPosition+1 == player.currentWindowIndex){
   Toast.makeText(this,"next clicked",Toast.LENGTH_SHORT).show()

                    }

Best approach for intercepting the exoplayer's control UI events is to add a custom dispatcher.

mBinding.styledPlayerView.setControlDispatcher(CustomControlDispatcher())

class CustomControlDispatcher(): DefaultControlDispatcher() {

    override fun dispatchFastForward(player: Player): Boolean {
        Logger.d(TAG, "dispatchFastForward")
        return super.dispatchFastForward(player)
    }

    override fun dispatchRewind(player: Player): Boolean {
        Logger.d(TAG, "dispatchRewind")
        return super.dispatchRewind(player)
    }

}

For rewind and Forward feature, I think this might help you >> Link

I use this link to add the Rewind and Forward Feature in ExoPlayer or ExoPlayer2. You can implement your own logic to track all of these Events. It works for me.

Related