Content player plugins on Android

This page explains how to create a custom content player plugin if the player you are using is not supported by the sample plugins found on our Git repository.

  1. Overview
  2. Sample content player plugins
  3. Create a custom content player plugin

Overview

To be able to trigger ad breaks at the right moment, the SVSAdManager needs to monitor your content's playback.

This is made possible through a content player plugin that makes the connection to your content player. A content player plugin is a class that implements the SVSContentPlayerPlugin interface. This interface contains 6 methods:

  • a method that will be called when the ad break starts
  • a method that will be called when the ad break ends
  • a method that must returns if the content player is currently playing
  • a method that must returns the content duration
  • a method that must returns the current position in the content video
  • a method that must returns the container where the ad player view must be added during ad breaks

Smart provides open source content player plugins for the most used content players, and you can also create your own plugin very easily.

Sample content player plugins

For your convenience, Smart provides some open sourced content player plugin on Github.

You can download them on the public repository.

You will find ready to use content player plugins for:

  • ExoPlayer
  • VideoView

These plugins are plug and play. Download the java file corresponding to your content player and add it to your project sources, initialize it as documented in the plugin and you'll be able to pass it to the SVSAdManager when starting it.

Creating a custom content player plugin

If you use another content player, you will have to create your own content player plugin.

It is pretty straightforward, you just have to create a class that implements the SVSContentPlayerPlugin interface.


// Suppose that your content player exposes these methods.
// (this will change depending on which player you useā€¦)
interface MyPlayer {
    void pause();
    void resume();
    boolean isStopped();
    boolean isPlaying();
    long getContentDuration();
    long getCurrentPosition();
}

// With a player like MyPlayer, your custom player plugin will be as follows:
public class MyCustomPlayerPlugin implements SVSContentPlayerPlugin {

    private MyPlayer myPlayer;
    private ViewGroup contentPlayerContainer;
    private boolean isLiveContent;

    public MyCustomPlayerPlugin(MyPlayer myPlayer, ViewGroup contentPlayerContainer, boolean isLiveContent) {
        this.myPlayer = myPlayer;
        this.contentPlayerContainer = contentPlayerContainer;
        this.isLiveContent = isLiveContent;
    }

    // Interface implementation

    @Override
    public void adBreakEnded() {
        // when a ad break ends, you might want to resume your player except if you have already played the content until the end
        if (!myPlayer.isStopped()) {
            myPlayer.resume();
        }
        // for some players, you will probably have to show the controls
    }

    @Override
    public void adBreakStarted() {
        // when an ad break ends, you need to pause your content player
        myPlayer.pause();
        // for some players, you will probably have to hide the controls
    }

    @Override
    public boolean isPlaying() {
        // the SVSAdManager needs to retrieve the current playback status of the content player
        return myPlayer.isPlaying();
    }

    @Override
    public long getContentDuration() {
        if (isLiveContent) {
            // if duration is unknown (for live content), you must return -1 and you will only be able to use 'interval based' ad rules
            return -1;
        } else {
            // otherwise you have to return the actual content duration
            return myPlayer.getContentDuration();
        }
    }

    @Override
    public long getCurrentPosition() {
        // return the current position in the content video
        return myPlayer.getCurrentPosition();
    }

    @Override
    public ViewGroup getContentPlayerContainer() {
        // return the container in which the ad player view will be added during ad breaks
        return contentPlayerContainer;
    }
}

Note that you might want to distinguish between content with known duration and (streamed) content with unknown duration directly at initialization. This is very important for ad breaks to be displayed properly when you stream live content.