Assigning a Video to the Player Programmatically

Product(s)
Video Cloud
Role(s)
Player Developer
Task(s)
Publish Videos

In this topic, you will learn how to dynamically change the video in the player. To do this you will programmatically change the player instances. This document shows various approaches to changing the video in the player.

Set video ID via URL

It is possible to use the URL implementation and dynamically change the video that is played. To do this you use a query string at the end of the URL in the form ?videoId=YOUR_VIDEO_ID as shown in the following example.

https://players.brightcove.net/..._default/index.html?videoId=2114345471001

Using reference IDs

In all examples in this document where a video ID is used a reference ID can be substituted. If you use the reference ID you must prepend that identifier with ref:. For example, the first URL uses the video ID, whereas the second uses the reference ID.

https://players.brightcove.net/..._default/index.html?videoId=2114345471001
https://players.brightcove.net/..._default/index.html?videoId=ref:1234abcd

Set video ID via tag attribute

The In-page embed code contains the <video> tag. The Video Cloud ID for the video is associated with the data-video-id HTML 5 data attribute.

<video-js
  data-account="1507807800001"
  data-player="dadff0fb-5635-4eac-a1b8-ab8c37a72be9"
  data-embed="default"
  data-video-id="2114345471001"
  class="video-js"
  controls></video-js>
<script src="https://players.brightcove.net/1507807800001/dadff0fb-5635-4eac-a1b8-ab8c37a72be9_default/index.min.js"></script>

Set video using JavaScript

In many cases, you want user interactions to control which video is played. To accomplish this, you can use JavaScript and the player catalog's getVideo() and load() methods to dynamically retrieve and play the desired video.

  • Lines 1-8: Advanced (In-page embed) code with no value for the data-video-id attribute.
  • Lines 11,17: Use the ready() method to be sure player is ready to act upon.
  • Line 12: Associate the player, referred to as this in the function, with a variable.
  • Line 13,16: Use the catalog's getVideo() method using the Video Cloud ID and a callback function as parameters.
  • Line 15: Use the catalog's load() method to load the video and play it.
  <video-js id="myPlayerID"
    data-account="1507807800001"
    data-player="3bc25a34-086a-4060-8745-dd87af3d53b4"
    data-embed="default"
    data-video-id=""
    class="video-js"
    controls></video-js>
  <script src="https://players.brightcove.net/1507807800001/3bc25a34-086a-4060-8745-dd87af3d53b4_default/index.min.js"></script>

  <script type="text/javascript">
    videojs.getPlayer('myPlayerID').ready(function() {
      var myPlayer = this;
      myPlayer.catalog.getVideo('2114345471001', function(error, video) {
        //deal with error
        myPlayer.catalog.load(video);
      });
    });
  </script>

Set video in iframe

To specify the video to a player hosted in an iframe, you can pass the video ID (or reference ID as mentioned earlier) via a URL's query string. For example:

  <iframe src='https://players.brightcove.net/.../index.html?videoId=2114345471001'
    allowfullscreen
    webkitallowfullscreen
    mozallowfullscreen></iframe> 

You can also use JavaScript to change the query string for the iframe's src attribute to tell the player which new video to play.

  • Lines 1-2: The iframe implementation code. Note the query string (text after the ? character) supplies the ID for the video to be played.
  • Line 5: Define the function to be called when video should change.
  • Line 6: Use JavaScript's getElementsByTagName() method to place the <iframe> tag into a variable. Note that the method returns an array of all matching elements on the page, and since you are using the first (and only) iframe tag on the page, you can use the zeroth element of the array.
  • Line 7: Creates a query string that will replace the old one. This defines the new video to be played.
  • Line 8: Assigns to a variable the src property of the iframe tag.
  • Line 9: Extracts the source for the player, minus the old query string.
  • Line 10: Builds the new value for the src property by combining the player source with the new video's query string definition.
  • Line 11: Assigns the new source/video information to the <iframe> tag's src property.
<iframe src='https://players.brightcove.net/...c_default/index.html?videoId=4341341161001'
    allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>

<script type="text/JavaScript">
  function changeVideo() {
    var iframeTag = document.getElementsByTagName("iframe")[0],
      newVideo = "?videoId=3742256815001",
      theSrc = iframeTag.src,
      srcWithoutVideo = theSrc.substring( 0, theSrc.indexOf( "?" ) ),
      newSrc = srcWithoutVideo + newVideo;
    iframeTag.src = newSrc;
  }
</script>