videojs-contrib-ads

Integrator Docs | Developer Docs | Github

API Reference

This page contains reference documentation for the interaction points between videojs-contrib-ads and ad plugins that use it. All methods are called on player.ads; for example, player.ads.isInAdMode(). All events are triggered or listened to on the player object; for example player.trigger('nopreroll') or player.on('readyforpreroll', () => {...}).

Informational methods and events

How contrib-ads talks to your ad plugin

Your ad plugin will listen to these events to trigger its behaviors. See Getting Started for more information.

How your ad plugin talks to contrib-ads

Your ad plugin can invoke these methods and events to play (or skip) ads. See Getting Started for more information.

Advanced Properties

Once the plugin is initialized, there are a couple properties you can access modify its behavior.

contentSrc

In order to detect changes to the content video, videojs-contrib-ads monitors the src attribute of the player. If you need to make a change to the src attribute during content playback that should not be interpreted as loading a new video, you can update this property with the new source you will be loading:

// you might want to switch from a low bitrate version of a video to a
// higher quality one at the user's request without forcing them to
// re-watch all the ad breaks they've already viewed

// first, you'd update contentSrc on the ads plugin to the URL of the
// higher bitrate rendition:
player.ads.contentSrc = 'movie-high.mp4';

// then, modify the src attribute as usual
player.src('movie-high.mp4');

disableNextSnapshotRestore

Advanced option. Prevents videojs-contrib-ads from restoring the previous video source.

If you need to change the video source during an ad break, you can use disableNextSnapshotRestore to prevent videojs-contrib-ads from restoring the snapshot from the previous video source.

if (player.ads.inAdBreak()) {
    player.ads.disableNextSnapshotRestore = true;
    player.src('another-video.mp4');
}

error

Advanced function. Gets, sets, and clears a non-critical ads error in videojs-contrib-ads.

The error will be in the form of an object. This object must contain a errorType string, and can also include zero to many custom properties.

{
  errorType: 'ads-error-type',
  // The key can have any name, and the value can have any type of data.
  customData: 'custom-data'
  ...
}

To get the current ads error, the error function should be called with no parameters. Returns null if there is no error.

const currentAdError = player.ads.error();

To set the current ads error, the error function should be called with a valid error.

const adError = { errorType: 'ads-error-type' }

player.ads.error(adError);

To clear the current ads error, the error function should be called with a null value. This value will also be cleared if an invalid error is passed to the function.

player.ads.error(null);

Deprecated

The following are slated for removal from contrib-ads and will have no special behavior once removed. These should no longer be used in integrating ad plugins. Replacements are provided for matching functionality that will continue to be supported.