Brightcove Player Sample: Playback Rate Adjuster

Product(s)
Video Cloud
Brightcove Player
Role(s)
Player Developer
Topic(s)
Code Samples
Playback
Type
Support Doc

In this topic, you will learn how to add a playback rate button to the Brightcove Player control bar, and use middleware to properly display the current time and video duration as you switch between playback speeds.

Player example

Start video playback and select the playback rate button in the control bar (default is 1x). Choose a different rate to see how video playback speed changes. Notice that when you change the playback rate, the duration time displayed is re-calculated based on the selected rate.

See the Pen Playback Rate Adjuster by Brightcove Learning Services (@bcls1969) on CodePen.

View the source code.

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.

Development sequence

Here is the recommended development sequence:

  1. Use the In-Page embed player implementation to test the functionality of your player, plugin and CSS (if CSS is needed)
  2. Put the plugin's JavaScript and CSS into separate files for local testing
  3. Deploy the plugin code and CSS to your server once you have worked out any errors
  4. Use Studio to add the plugin and CSS to your player
  5. Replace the In-Page embed player implementation if you determine that the iframe implementation is a better fit (detailed in next section)

For details about these steps, review the Quick Start: Plugin Development guide.

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.

Middleware functions

This sample uses Video.js middleware to implement the code solution. At a very high level, the use cases where middleware is of most value is when you want to intercept and alter core behaviors of the player, such as setting the source or current time. Video.js middleware in general is not discussed at length in this document, but the actual code used for the middleware function is explained. Here are documents from Video.js sources that do detail middleware:

Implementing middleware functions with Brightcove Player

The simplest way to use a middleware function is to:

  1. Write the middleware function.
  2. Place the middleware function either on the page with the player in a script tag, or include the middleware function using the src attribute with a script tag. Both implementations will be shown in this document.
  3. Register the middleware function with the player using the use() method, as shown here:
    videojs.use('mimeType', functionName);

    For the mimetype you can use a standard video mimetype, like video/mp4, or use an * if you wish the middleware function to act on all mimetypes.

API/Plugin resources used

API Classes API Methods
ControlBar controlBar.addChild()
PlaybackRateMenuButton  

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

No special configuration is required for the Brightcove Player you create for this sample.

Other HTML

Remember to add the id attribute to the video tag in the player embed code.

<video-js id="myPlayerID"
...

Application flow

The basic logic behind this application is:

  • Define the middleware functions
  • Register the middleware with the player
  • Define the playback rate options
  • Initialize the playback rate button

Define the middleware functions

Find the code which is labeled:

// +++ Define the middleware functions +++

Create the following middleware functions:

  1. Create a supporting middleware function that will re-calculate the playback start and end times based on the selected playback rate.
  2. Create a middleware function that will update the start time, duration and even current time when seeking.

    The basic syntax for creating a middleware function is as follows:

    const playbackrateAdjuster = function(player) {
       return {
       ...
     };

    Note that the player object is passed to the function.

    This middleware function does the following:

    1. Listen for the ratechange event: When the playback rate is changed, then trigger the durationchange and timeupdate events.

    2. Implement the setSource() method: This is a required method for every middleware function. By using the syntax shown means the middleware will always be selected, irregardless of the video type:
      setSource(srcObj, next) {
        next(null, srcObj);
      },

    3. Override methods using current playback rate: Override the methods for duration, currentTime, buffered, seekable and played with values calculated using the current playback rate.

Register the middleware with the player

Find the code which is labeled:

// +++ Register the middleware with the player +++

Register the middleware function with the player using the use() method.

Define the playback rate options

Find the code which is labeled:

// +++ Define the playback rate options +++

Define the playback rates that you want displayed in the playback rate menu button.

Initialize the playback rate button

Find the code which is labeled:

// +++ Initialize the playback rate button +++

Add the playback rate menu button to the player control bar, and initialize it with the values specified in the options object.

Application styling

There are no additional styles for this sample.

Using the middleware function

Once the middleware function has been written you want to use it. Two ways were mentioned earlier, and will be detailed now.

Code on same page as player

This is where you define the middleware function on the same page as the player, and then register it. The player example in the Codepen above uses this method.

Code in a separate JavaScript file

In the following code, the JavaScript for the middleware functions and the use() method to register it are simply included in the page.

<video-js id="myPlayerID"
	  data-video-id="5604123528001"
	  data-account="1752604059001"
	  data-player="default"
	  data-embed="default"
	  data-application-id
	  class="video-js"
	  width="640" height="360"
	  controls muted></video>
	<script src="https://players.brightcove.net/1752604059001/default_default/index.min.js"></script>

  <!-- Include the rate adjuster middleware functions -->
  <script src="rate-adjuster.js"></script>

  <script type="text/javascript">
  	videojs.getPlayer("myPlayerID").ready(function(){
  		// When the player is ready, get a reference to it
  		var myPlayer = this;
  		console.log("myPlayer",myPlayer)
  		// Define the playback rate options
  		options = {"playbackRates":[0.5, 1, 1.5, 2, 4]};
  		// Initialize the playback rate button
  		myPlayer.controlBar.playbackRateMenuButton = myPlayer.controlBar.addChild('PlaybackRateMenuButton', {
      	playbackRates: options.playbackRates
  	  });
    });
  </script>

This example assumes that you will host the JavaScript file on your own server. For the complete code, see the plugin section in the GitHub repo.