Brightcove Player Sample: Scrolling Player into View with Ads

In this topic, you will learn how to use a Brightcove Player which starts playing the video or ad as soon as you fully scroll the player into view, and pauses the video or ad when you scroll it out of view. The Brightcove Player API play() and pause() methods are called to start and stop the video based on the position of the player.

Player example

Scroll the player in and out of view. When the player is fully scrolled into view, the video or ad will start playing. When you scroll the player out of view, the video or ad will stop playing. In this example there is a pre-roll, a skippable mid-roll at five seconds, and a post-roll.

See the Pen 18352-scrolling-player-view-ads by Brightcove Learning Services (@rcrooks1969) on CodePen.

Source code

View the complete solution on GitHub.

Using the CodePen

Here are some tips to effectively use the above CodePen:

  • Toggle the actual display of the player by clicking the Result button.
  • Click the HTML/CSS/JS buttons to display ONE of the code types.
  • Later in this document the logic, flow and styling used in the application will be discussed in the Player/HTML configuration, Application flow and Application styling sections. The best way to follow along with the information in those sections is to:
    1. Click the EDIT ON CODEPEN button in the CodePen and have the code available in one browser/browser tab.
    2. In CodePen, adjust what code you want displayed. You can change the width of different code sections within CodePen.
    3. View the Player/HTML configuration, Application flow and/or Application styling sections in another browser/browser tab. You will now be able to follow the code explanations and at the same time view the code.

API/Plugin resources used

API Methods Brightcove Player Plugins
play() IMA3 Plugin

A key JavaScript event used in this code is onscroll. This event is dispatched, and in this code handled, every time the window element is scrolled.

Player/HTML configuration

This section details any special configuration needed during player creation. In addition, other HTML elements that must be added to the page, beyond the in-page embed player implementation code, are described.

Player configuration

The muted attribute has been added to the player so as to avoid autoplay issues. See the Autoplay Considerations document for details.

Other HTML

An HTML <script> tag is used to import the jQuery library and the JavaScript for the IMA3 Plugin. In the HTML's head section the IMA3's CSS is imported using an HTML <link> tag.

Application flow

The basic logic behind this application is:

  • Configure the IMA3 Plugin.
  • Listen for scroll events.
  • When a scroll event is handled, check if the player is in the viewport or not.
  • If the player in the viewport, play the video, if it is not, pause the video.
  • Track ad events to determine if the normal Brightcove Player or ad player is in current use.

Configure IMA3 plugin

Find the code which is labeled:

// ### Configure IMA3 plugin

The IMA3 plugin is configured to use a VMAP configuration that will play a pre-, mid- and post-roll.

Listen for any scroll events on the window element

Find the code which is labeled:

// ### Execute every time page is scrolled ###

This one line of code calls the checkIfVideoInView() method on every window.onscroll event dispatch.

Handle the scroll events

Find the code which is labeled:

// ### Called on scroll, check if in view and the play/pause ###

The checkIfVideoInView() event handler function generally checks if the player is in the viewport, then either plays or pauses the video. It is critical that the code determine which player is being used, the normal player or the ad player. The Boolean value isAdPlaying is toggled based on ad events (in the event handler at the bottom of the code), then a conditional (ternary) operator is used to assign the currentPlayer variable the correct player. The isScrolledIntoView() method is used in an if statement to check if the player is in the viewport. Naturally, the isScrolledIntoView() method returns a Boolean value.

Check if player is entirely in the viewport

Find the code which is labeled:

// ### Checks if player is in view ###

This function, which returns a Boolean value, uses jQuery to determine if the player is entirely in the viewport.

Track which player is being used

Find the code which is labeled:

// ### Set Boolean value for if ad is playing ###

A number of ad events are monitored and the Boolean value is set accordingly.

Application styling

The only CSS sets the player size.

Plugin code

Normally when converting the JavaScript into a Brightcove Player plugin nominal changes are needed. One required change is to replace the standard use of the ready() method with the code that defines a plugin.

Here is the very commonly used start to JavaScript code that will work with the player:

videojs.getPlayer('myPlayerID').ready(function() {
  var myPlayer = this;

You will change the first line to use the standard syntax to start a Brightcove Player plugin:

videojs.registerPlugin('pluginName', function(options) {
  var myPlayer = this;

As mentioned earlier, you can see the plugin's JavaScript code in this document's corresponding GitHub repo: scroll-into-view-ads.js.

Using the plugin with a player

Once you have the plugin's CSS and JavaScript files stored in an Internet accessible location, you can use the plugin with a player. In Studio's PLAYERS module you can choose a player, then in the PLUGINS section add the URLs to the CSS and JavaScript files, and also add the Name and Options, if options are needed.