Skip to content

7. Recipes

Nam Nguyen Hoai edited this page Oct 25, 2016 · 16 revisions

7.1 Custom Playback Strategy

By implementing the interface ToroStrategy, apply your own logic in required methods, you can have your own Strategy. Built-in ones can be used as delegation. See below for a sample:

public class PlayFromTopStrategy implements ToroStrategy {

  private final int firstMediaPosition; // Should be the first Media obtained item from Adapter

  private boolean isFirstVideoObserved = false;
  private final ToroStrategy delegate = Toro.Strategies.FIRST_PLAYABLE_TOP_DOWN;

  public PlayFromTopStrategy(int firstMediaPosition) {
    this.firstMediaPosition = firstMediaPosition;
  }

  @Override public String getDescription() {
    return "All Media should be played by order, start from 'firstMediaPosition'";
  }

  @Nullable @Override public ToroPlayer findBestPlayer(List<ToroPlayer> candidates) {
    // Inherit the same behaviour with built-in one.
    return delegate.findBestPlayer(candidates);
  }

  @Override public boolean allowsToPlay(ToroPlayer player, ViewParent parent) {
    boolean allowToPlay = (isFirstVideoObserved || player.getPlayOrder() == firstMediaPosition)  //
        && delegate.allowsToPlay(player, parent);
    // Keep track of first video on top.
    if (player.getPlayOrder() == firstMediaPosition) {
      isFirstVideoObserved = true;
    }
    return allowToPlay;
  }
}

7.1 Create a Custom Extension

Here is the tutorial about how to create your own Toro Extension.

As can be seen from the library structure, each extension is implemented in a separated way, which means that anyone can do the same thing. The following is a check list for those when building their own Extension for Toro:

  • A good motivation: built-in Extensions are not enough, there are better Media Playback APIs out there, ...

  • When you have a good motivation, now build your own PlayerView - a custom View which can be used to display the Media Content (well, just another VideoView, or SimpleExoPlayerView, ...). If you build your Extension on top of built-in one, it is good to use my built-in Views, or customize it by your own will.

  • Create the extension of PlayerViewHelper: as said, this class stays between the ViewHolder, the Media Playback API and Toro's internal API. A ViewHolder will require it to connect to all benefit of Toro as well as the Playback API. My built-in Helpers can be re-used as delegation as well.

  • Create the ViewHolder: this is the main character. You will need to create a ViewHolder and setup its PlayerViewHelper instance there, to connect your ViewHolder with your Playback API as well as Toro internal API.

  • Optional: extends the MediaPlayerManager. This can be useful when you need to have access to the Manager, or you have better way to handle the position cache (use native cache, maybe). See TimelineAdapter.java to see how I create a simple one.

Clone this wiki locally