On This Page

Brightcove Player Sample: Background Video Random Playlist

In this topic, you will learn how to use a playlist to supply randomly selected background videos.

Player example

In the player you will see videos randomly selected from a playlist that continually play as background videos. Also, at the start of each video the title and description of each video are displayed overlaying the background video.

iframe or In-Page embed

When developing enhancements for the Brightcove player you will need to decide if the code is a best fit for the iframe or In-Page embed implementation. The best practice recommendation is to build a plugin for use with an iframe implementation. The advantages of using the iframe player are:

No collisions with existing JavaScript and/or CSS

Automatically responsive

The iframe eases use in social media apps (or whenever the video will need to "travel" into other apps)

Although integrating the In-Page embed player can be more complex, there are times when you will plan your code around that implementation. To generalize, this approach is best when the containing page needs to communicate to the player. Specifically, here are some examples:

Code in the containing page needs to listen for and act on player events

The player uses styles from the containing page

The iframe will cause app logic to fail, like a redirect from the containing page

Even if your final implementation does not use the iframe code, you can still use the In-Page embed code with a plugin for your JavaScript and a separate file for your CSS. This encapsulates your logic so that you can easily use it in multiple players.

API/Plugin resources used

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

Be sure when you create the player the following settings are implemented:

Set responsiveSet playlistSet autostart and play when selectedSet skip to next video in a playlist

Other HTML

First, the in-page embed player code is nested in TWO HTML div elements. Both of the divs contain the appropriate styles to fill the entire browser window, and behave responsively. Next, another div element is used to define where the text that is placed over the background video should be located.

Application flow

The basic logic behind this application is:

Appropriately style the player and box that contains the overlaid text.

Before the playlist is placed in the player, using the duringplaylistchange event, randomize the playlist using the player.playlist.shuffle() method.

Set the playlist to repeat indefinitely.

On the load of every video from the playlist display the video title and short description.

Randomize the playlist

Find the code which is labeled:

// +++ Shuffle (randomize) the videos +++

The on() method is used to listen for the duringplaylistchange event to be dispatched. This lets you randomize the playlist BEFORE it is placed in the player. If you don't do this you will either see the playlist reflect the shuffling (different videos' posters will quickly be shown in the player), or the poster for the first video in the un-shuffled playlist will flash in the player.

Set the playlist to repeat

Find the code which is labeled:

// +++ Set the playlist to repeat +++

The repeat() method from the Playlist API is used to keep the playlist playing indefinitely.

Display the title and description

Find the code which is labeled:

// ### Display the title and description ###

An event handler is used to wait for the loadstart event. This is done as the mediainfo object is not populated until that event is dispatched. Then the name and description are injected into the HTML.

Application styling

The CSS performs the following tasks:

Makes the background color of the window black.

Hides the player controls for the background video player.

Disables pointer events on the player.

Styles the box that overlays the text. Depending on the size of your player and the amount of content display these styles may need to be adjusted.

Styles the h1 used in the overlay text.

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:

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.