[object Object] Icon

Encoding
Learn how to create, start, manage and modify Encodings

[object Object] Icon

Player
Learn how to create, start, manage and modify Players

[object Object] Icon

Analytics
Learn how to create, start, manage and modify Analyticss

Docs Home
User shortcuts for search
Focus by pressing f
Hide results by pressing Esc
Navigate via   keys

Wed Feb 23 2022

Setting up Ads with the Web Player

Introduction to AdvertisingLink Icon

Bitmovin Player is capable of serving Ads out of the box. This guide is made to quickly help get you started on configuring it with your integration.

There are two ways that Ads can be configured with Bitmovin Player. One option is to use static configuration in the player config object, and the other is to schedule them dynamically using PlayerAdvertisingAPI.

We will cover both cases in this tutorial. However, in case you are new to the whole advertising topic, we will start by covering terms that will often appear along all the advertising related docs. If you are already familiar with these basic concepts feel free to jump to the next section.


The basic unit of the advertising API is called an AdBreak. It represents a specific time slot within the streamed content dedicated to ads playback. AdBreaks are configured with a position and a tag where the position refers to a specific point on the stream timeline while the tag contains information about which sources that AdBreak should load Ads from. Typically this is a URL pointing to your Ad server that will return a response in a supported format: VAST, VMAP or VPAID. You can check IAB Tech Lab resources to learn more about these formats.

When the Bitmovin Player Advertising Module starts to play an AdBreak at its position, it unloads the main content and plays the advertisement using the player API. However, this switching between ads and main content does not count as a player impression. The ad playback is started automatically and there are also AdBreakStarted and AdBreakFinished player events triggered to signal this transition. This is because during the ad playback all the calls to player API will only return values related to that specific advertising content.


Now that we have covered the basic concepts, let's explore how to setup such an AdBreak with your content.

Static Ads ConfigurationLink Icon

The easiest way to configure Ads is by adding the advertising property to the player configuration object. All that needs to be provided is a URL pointing to a target Ad server along with the type of advertising tag. We will also use the Bitmovin Advertising Module to gain more control and additional features.

Simple Pre-roll with Static Configuration

The following example showcases this, and will play the configured AdBreak as a pre-roll. "Pre-roll" means that the ad will start playing before the main content, and is the default value for the position property, if it is not explicitly defined.

1var playerConfig = {
2 key: '<PLAYER_LICENSE_KEY>',
3 advertising: {
4 adBreaks: [
5 {
6 tag: {
7 type: 'vast',
8 // More Ad Samples can be found here:
9 // https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags
10 url:
11 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator='
12 }
13 }
14 ]
15 }
16};
17
18// By default, the Bitmovin Player uses the IMA Advertising Module.
19// Here we activate the Bitmovin Advertising Module instead, which gives you more control.
20bitmovin.player.Player.addModule(window.bitmovin.player['advertising-bitmovin'].default);
21
22var player = new bitmovin.player.Player(document.getElementById('player'), playerConfig);

Run example in CodeSandbox

Dynamic Ads SchedulingLink Icon

To gain more flexibility, it is also possible to schedule an AdBreak dynamically in code using the PlayerAdvertisingAPI exposed by the Bitmovin Player instance. To do this, you need to call the player.ads.schedule method.

This method expects an object with the following layout:

  • tag: an AdBreakConfig containing an Ad tag
  • id: a user-defined Id
  • position: the string 'pre', the string 'post' or a string with numeric value representing the break time position.

Please note that in order to activate this API, at least an empty object assigned to advertising property needs to be present in the player configuration.

During the ad playback it is possible to call player.ads.skip method to skip the Ad currently being played.

More details on the Bitmovin Player Advertising API is available here.

Start Playback at Specific Position

Here is a first simple example of dynamic AdBreak scheduling where Ad playback is positioned to the 15th second of main content.

1var playerConfig = {
2 key: '<PLAYER_LICENSE_KEY>',
3 // This won't configure any ads but it will make the PlayerAdvertisingAPI available
4 advertising: {},
5};
6
7// By default, the Bitmovin Player uses the IMA Advertising Module.
8// Here we activate the Bitmovin Advertising Module instead, which gives you more control.
9bitmovin.player.Player.addModule(window.bitmovin.player['advertising-bitmovin'].default);
10var player = new bitmovin.player.Player(document.getElementById('player'), playerConfig);
11
12player.ads.schedule({
13 tag: {
14 url: 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=',
15 type: 'vast'
16 },
17 id: 'Ad',
18 position: '15',
19});

Run example in CodeSandbox

Schedule Immediate AdBreak

It is also possible to control the advertising flow manually and schedule the AdBreak to be played immediately based on some event. In the following example we react to the click of a button to schedule an immediate ad break.

1var playerConfig = {
2 key: '<PLAYER_LICENSE_KEY>',
3 // This won't configure any ads but it will make the PlayerAdvertisingAPI available
4 advertising: {},
5};
6
7// By default, the Bitmovin Player uses the IMA Advertising Module.
8// Here we activate the Bitmovin Advertising Module instead, which gives you more control.
9bitmovin.player.Player.addModule(window.bitmovin.player['advertising-bitmovin'].default);
10var player = new bitmovin.player.Player(document.getElementById('player'), playerConfig);
11
12function scheduleImmediateAdBreak() {
13 // Current playback position of the main content
14 var currentTime = player.getCurrentTime();
15 player.ads.schedule({
16 tag: {
17 url: 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=',
18 type: 'vast'
19 },
20 id: 'Ad',
21 position: currentTime.toString(),
22 });
23}
24
25// Use a button to schedule an ad whenever its clicked
26var scheduleBtn = document.getElementById('scheduleBtn');
27skipBtn.addEventListener('click', function () {
28 scheduleImmediateAdBreak();
29});
30
31// Use this button to let the user skip the current ad
32var skipBtn = document.getElementById('skipButton');
33skipBtn.addEventListener('click', function () {
34 player.ads.skip();
35});

Run example in CodeSandbox

Schedule AdBreak After Specific Time of Playback

Dynamic ads scheduling gives you full control over the advertising configuration, and allows you to define custom logic around how often to play advertising. In the following advanced example we play the advertisement after a specific amout of playback time, rather than binding the AdBreak to a specific position in the timeline.

This is achieved by observing the player TimeChanged event to see how much time elapsed. As soon as the threshold is reached, we can trigger the Ad scheduling.

1var playerConfig = {
2 key: '<PLAYER_LICENSE_KEY>',
3 // This won't configure any ads but it will make the PlayerAdvertisingAPI available
4 advertising: {},
5};
6
7// By default, the Bitmovin Player uses the IMA Advertising Module.
8// Here we activate the Bitmovin Advertising Module instead, which gives you more control.
9bitmovin.player.Player.addModule(window.bitmovin.player['advertising-bitmovin'].default);
10var player = new bitmovin.player.Player(document.getElementById('player'), playerConfig);
11
12function scheduleImmediateAdBreak() {
13 // Current playback position of the main content
14 var currentTime = player.getCurrentTime();
15 player.ads.schedule({
16 tag: {
17 url: 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=',
18 type: 'vast'
19 },
20 id: 'Ad',
21 position: currentTime.toString(),
22 });
23}
24
25// Observe player `TimeChanged` event and schedule the immediate `AdBreak` when it reached the threshold
26var timePlayed = 0;
27player.on('timechanged', function(event) {
28 if (timePlayed >= 15) {
29 scheduleImmediateAdBreak();
30 timePlayed = 0;
31 } else {
32 timePlayed = event.time;
33 }
34});

Run example in CodeSandbox

What's nextLink Icon

Give us feedback