Example 3: adding buttons for choosing the subtitle/caption track

You may have noticed that with many browsers, the standard implementation of the video element does not let the user choose the subtitle language…

Read this article by Ian Devlin about the current status of multiple WebVTT track support by the different browsers, as at May 2015. Note that currently (July 2016), neither Chrome nor FireFox offers a menu to choose the track to display. 

However, it’s easy to implement this feature using the Track API.

Here is a simple example at JSBin: we added two buttons below the video to enable/disable subtitles/captions and let you choose which track you prefer. 

Buttons for choosing the track/language under a standard video player

HTML code:

  1. <body onload=”init()”>
  2.  …
  3. <video id=”myVideo” preload=”metadata” controls crossOrigin=”anonymous” >
  4.      <source src=”https://&#8230;../elephants-dream-medium.mp4″
  5.              type=”video/mp4″>
  6.      <source src=”https://&#8230;../elephants-dream-medium.webm”
  7.              type=”video/webm”>
  8.      <track  label=”English subtitles”
  9.              kind=”subtitles”
  10.              srclang=”en”
  11.              src=”https://&#8230;../elephants-dream-subtitles-en.vtt”
  12.              default>
  13.      <track  label=”Deutsch subtitles”
  14.              kind=”subtitles”
  15.              srclang=”de”
  16.              src=”https://&#8230;../elephants-dream-subtitles-de.vtt”>
  17.      <track  label=”English chapters”
  18.              kind=”chapters”
  19.              srclang=”en”
  20.              src=”https://&#8230;../elephants-dream-chapters-en.vtt”>
  21. </video>
  22. <h3>Current track: <span id=”currentLang”></span></h3>
  23. </section>

JavaScript code:

  1. var langButtonDiv, currentLangSpan, video;
  2.  
  3. function init() {
  4.    langButtonDiv = document.querySelector(“#langButtonDiv”);
  5.    currentLangSpan = document.querySelector(“#currentLang”);
  6.    video = document.querySelector(“#myVideo”);
  7.    console.log(“Number of tracks = “
  8.                + video.textTracks.length);
  9.    // Updates the display of the current track activated
  10.    currentLangSpan.innerHTML = activeTrack();
  11.    // Build the buttons for choosing a track
  12.    buildButtons();
  13. }
  14.  
  15. function activeTrack() {
  16.    for (var i = 0; i < video.textTracks.length; i++) {
  17.       if(video.textTracks[i].mode === ‘showing’) {
  18.          return video.textTracks[i].label + ” (“
  19.                 + video.textTracks[i].language + “)”;
  20.       }
  21.    }
  22.    return “no subtitles/caption selected”;
  23. }
  24.  
  25. function buildButtons() {
  26.    if (video.textTracks) { // if the video contains track elements
  27.       // For each track, create a button
  28.       for (var i = 0; i < video.textTracks.length; i++) {
  29.          // We create buttons only for the caption and subtitle tracks
  30.          var track = video.textTracks[i];
  31.          if((track.kind !== “subtitles”) && (track.kind !== “captions”))
  32.             continue;
  33.          // create a button for track number i         
  34.          createButton(video.textTracks[i]); 
  35.       }
  36.    }
  37. }
  38. function createButton(track) {
  39.    // Create a button
  40.    var b = document.createElement(“button”);
  41.    b.value=track.label;
  42.    // use the lang attribute of the button to keep trace of the
  43.    // associated track language. Will be useful in the click listener
  44.    b.setAttribute(“lang”, track.language); 
  45.    b.addEventListener(‘click’, function(e) {
  46.      // Check which track is the track with the language we’re looking for
  47.      // Get the value of the lang attribute of the clicked button
  48.      var lang = this.getAttribute(‘lang’); 
  49.      for (var i = 0; i < video.textTracks.length; i++) {
  50.        if (video.textTracks[i].language == lang) {
  51.           video.textTracks[i].mode = ‘showing’;
  52.        } else {
  53.           video.textTracks[i].mode = ‘hidden’;
  54.        }
  55.      }
  56.      // Updates the span so that it displays the new active track
  57.     currentLangSpan.innerHTML = activeTrack();
  58.   });
  59.   // Creates a label inside the button
  60.   b.appendChild(document.createTextNode(track.label));
  61.   // Add the button to a div at the end of the HTML document
  62.   langButtonDiv.appendChild(b);
  63. }

External resource: If you are interested in building a complete custom video player, MDN offers an online tutorial with further information about styling and integrating a “CC” button. 

Leave a comment