How to add Listener for next , previous , rewind a

2019-05-06 11:56发布

问题:

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

回答1:

I use this link to customize the ExoPlayer for previous, next etc. You can implement your own logic to track all of these Events. It works for me.



回答2:

My approach

Assumed, we are already create collection of media source, DASH or HLS.

And using player view with SimpleExoPlayerView component. It will automatically have default control, prev, next, for example.

I handle collection of media source with current window index with this exoPlayer.getCurrentWindowIndex() to back and forth media source.

And you can check current index via this onTracksChanged method

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

currentIndex = exoPlayer.getCurrentWindowIndex();

}

You can look this file for implementation these control.

Hope this help.



回答3:

You might be able to add a ExoPlayer.Listener() and use constants from the PlaybackstateCompat class.

Override the onPlayerStateChanged listener and check the playbackState with rewind/fastforward/etc

 player.addListener(new ExoPlayer.Listener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (playbackState == PlaybackStateCompat.STATE_FAST_FORWARDING) {
                    //do something                 
                }
                if (playbackState == PlaybackStateCompat.STATE_REWINDING) {
                    //do something else               
                }
            }

            @Override
            public void onPlayWhenReadyCommitted() {

            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {
                mExoPlayer.stop();

            }
        });

Also:

  1. STATE_SKIPPING_TO_NEXT
  2. STATE_SKIPPING_TO_PREVIOUS