css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-- styles for the overlay plugin -->
<link href="https://players.brightcove.net/videojs-overlay/2/videojs-overlay.css" rel="stylesheet">

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

<!-- styles for the overlay plugin -->
<script src="https://players.brightcove.net/videojs-overlay/2/videojs-overlay.min.js"></script>
            
          
!
            
              /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #fff;
}
/*
 * Styles essential to the sample
 * are below
 */
/* styling for the video thumbnails */
.video-thumbnail {
  margin: 0;
  padding: 0;
  width: 190px;
  height: 107px;
  cursor: pointer;
}
/*override some of the default overlay styling*/
div.vjs-overlay.vjs-overlay-top-left
{
  width: 100%;
  left: 0;
}
            
          
!
            
              var myPlayer,
    requestData,
    apiBaseURL = 'https://edge.api.brightcove.com/playback/v1/accounts/',
    policyKey = "BCpkADawqM1eifBpAkEr4aJrH9i950qErQCg8FvHXBCigF0JjC-zZyhN4T1XGGGBbB0hojevaABtp54BTvT9Er0KplSpC6tqm8YgyCtIzGl5sc77i23GLWYdpLdtF7Aei45EuLqlUznlkiXU",
    videoData = [];

// +++ Wait for loadedmetadata +++
/**
  * wait for the video metadata to load
  */
videojs.getPlayer('myPlayerID').one('loadedmetadata', function(){
  myPlayer = this;
  // set up the Playback API request
  requestData = setRequestData();
  // make the Playback API request
  getRelatedVideos(requestData, function(relatedVideos) {
    // extract the needed video data into an array
    videoData = extractVideoData(relatedVideos);
    // generate the HTML for the overlay
    videoList = createVideoList(videoData);
    // add the overlay
    addOverlayText(videoList);
  });

});

// +++ Setup the API request +++
/**
  * set up the Playback API request
  */
function setRequestData() {
  var endPoint = '',
      accountId,
      videoName,
      requestURL,
      endPoint,
      tagValue,
      requestData = {},
      dataReturned = false;

  // use the mediainfo object to get the account id, tag value and video name for the video currently loaded in the player
  accountId = myPlayer.mediainfo.account_id;
  tagValue = myPlayer.mediainfo.tags[0];
  videoName = myPlayer.mediainfo.name;

  // add the account id and videos search term to the base URL. Note that your policy key needs to be enabled for search or you will get an error with the request.
  requestURL = apiBaseURL + accountId + '/videos';
  // return up to 9 videos which have a tag value equal to the current video, excluding the current video by name
  endPoint = '?q=tags:"' + tagValue + '" -name:"' + videoName + '"&limit=9';

  // add the query string to the request URL
  requestData.url = requestURL + endPoint;
  // set the request type to GET
  requestData.requestType = 'GET';
  return requestData;
}

// +++ Make the Playback API request +++
/**
  * request data from the Playback API
  */
getRelatedVideos = function(options, callback) {
  var httpRequest = new XMLHttpRequest(),
      responseData,
      parsedData,
      // response handler
      getResponse = function() {
        try {
          if (httpRequest.readyState === 4) {
            if (httpRequest.status >= 200 && httpRequest.status < 300) {
              responseData = httpRequest.responseText;
              parsedData = JSON.parse(responseData);

              callback(parsedData);
            } else {
              alert('There was a problem with the request. Request returned ' + httpRequest.status);
            }
          }
        } catch (e) {
          alert('Caught Exception: ' + e);
        }
      };
  // set the response handler
  httpRequest.onreadystatechange = getResponse;
  // open the request
  httpRequest.open('GET', requestData.url);
  // set the headers
  httpRequest.setRequestHeader('Accept', 'application/json;pk=' + policyKey);
  // open and send the request
  httpRequest.send();
};

// +++ Format the overlay content +++
/**
  * extract video data from Playback API response
  * @param {array} playbackData the data from the Playback API
  * @return {array} videoData array of video info
  */
function extractVideoData(playbackData) {
  var i,
      iMax = playbackData.videos.length,
      videoItem;

  for (i = 0; i < iMax; i++) {
    if (playbackData.videos[i].id !== null || playbackData.videos[i].thumbnail !== null) {
      videoItem = {};
      videoItem.id = playbackData.videos[i].id;
      videoItem.name = playbackData.videos[i].name;
      videoItem.thumbnail = playbackData.videos[i].thumbnail;
      videoData.push(videoItem);
    }
  }
  return videoData;
}

/**
  * create the html for the overlay
  * @param {array} videoData array of video objects
  * @return {HTMLElement} videoList the div element containing the overlay
  */
function createVideoList(videoData) {
  var i,
      iMax = videoData.length,
      videoList = createEl('div', {
        id: 'videoList'
      }),
      videoWrapper = createEl('div'),
      thumbnailLink,
      thumbnailImage;

  videoList.appendChild(videoWrapper);
  for (i = 0; i < iMax; i++) {
    thumbnailLink = createEl('a', {
      href: 'javascript:loadAndPlay(' + i + ')'
    })
    thumbnailImage = createEl('img', {
      class: 'video-thumbnail',
      src: videoData[i].thumbnail
    });
    videoWrapper.appendChild(thumbnailLink);
    thumbnailLink.appendChild(thumbnailImage);
  }
  return videoList;
}

/**
  * create an element
  *
  * @param  {string} type - the element type
  * @param  {object} attributes - attributes to add to the element
  * @return {HTMLElement} the HTML element
  */
function createEl(type, attributes) {
  var el,
      attr;

  el = document.createElement(type);
  if (attributes !== null) {
    for (attr in attributes) {
      el.setAttribute(attr, attributes[attr]);
    }
    return el;
  }
}

/**
  * add text content to an element
  * @param {HTMLElement} el the element
  * @param {string} str the text content
  */
function addText(el, str) {
  el.appendChild(document.createTextNode(str));
}

/**
  * intialize the overlay plugin with the related video thumbnails
  * @param {HTML} overlayContent the HTML for the overlay
  */
function addOverlayText(overlayContent) {
  myPlayer.overlay({
    overlays: [{
      content: overlayContent,
      start: 'pause',
      end: 'play'
    },
    {
      start: 'end',
      end: 'play'
     }]
  });
}

// +++ Load and play related video +++
/**
  * load and play a video
  * this function called when thumbnails in the overlay are clicked
  * @param {integer} idx the index of the video object in the videoData array
  */
loadAndPlay = function(idx) {
  var currentId = videoData[idx].id;
  myPlayer.catalog.getVideo(currentId, function(error, video) {
    try {
      myPlayer.catalog.load(video);
    } catch (e) {
      myPlayer.catalog.load(video);
    }
    myPlayer.play();
  });
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console