Exoplayer: Switch between video and view at given times

Created on 19 Aug 2020  路  4Comments  路  Source: google/ExoPlayer

I'm looking for a way to stop the video at, let's say 10s, pause the video switch to another view and when the user is finished with that view, start back the video display.

After looking at the dev website, I was wondering if this could be done with IMA ads but I guess it can't be done because I need indefinite time for the in-middle view.

Is there any way I can do this? Then is there a way to display little vertical yellow bars (at switching position) in the seekbar as you do for ads ?

question

All 4 comments

You can use ad markers for marking positions and fire player messages at the given playback positions to run your code:

long[] extraAdGroupTimesMs = new long[]{20_000, 40_000};
playerView.setExtraAdGroupMarkers(extraAdGroupTimesMs, new boolean[] { false, false});

The first argument is an array holding the positions on the timebar of the PlayerControlView. The second argument is an array of flags whether these markers should be shown or not.

You can customize the color of the markers by customizing the player_control_view.xml layout file and replacing the placeholder with id exo_progress_placeholder with a DefaultTimeBar element with id exo_progress:

  <com.google.android.exoplayer2.ui.DefaultTimeBar
    android:id="@id/exo_progress"
    android:layout_width="match_parent"
    android:layout_gravity="bottom"
    app:ad_marker_color="@color/customAdMarkerColor" />

And finally you can add player messages which can execute custom code at a given position:

for (int i = 0; i < extraAdGroupTimesMs.length; i++) {
  long positionMs = extraAdGroupTimesMs[i];
  PlayerMessage playerMessage = player.createMessage(
      (messageType, payload) -> {
        Log.d("player message", "message at position: " + positionMs);
        // do what you need to do
        player.setPlayWhenReady(false);
        switchToAnotherView();
      });

  playerMessage
      .setPosition(positionMs) // the playback position according to the markers
      .setDeleteAfterDelivery(false) 
      .setHandler(new Handler())
      .send();
}

Thank you @marcbaechinger, very helpful.

Just one thing to add here, I needed to use activity.runOnUiThread in the lambda or I would have an error saying that you need to be in the same thread for changing the player and the view.

Oh, yes, sorry. I forgot to add the setHandler(new Handler()) call. I added it to the snippet above. If you pass the handler, the message is executed on the thread on which the Handler has been created. Assuming you send the message on the UI thread this would do what you want.

I close this for now. Please re-open if you have any further question.

Was this page helpful?
0 / 5 - 0 ratings