Youtube Closed Caption for HTML5 Not Working
Asked Answered
S

1

0

I am having issues displaying closed caption on youtube videos using the iframe player.

Here's the code I am using:

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      function onPlayerStateChange(event) {
        event.target.loadModule('captions');
      }
    </script>
  </body>
</html>

Here's a gist https://gist.github.com/khirakawa/0a81b3039a85b9875b59

This is basically the same sample code that's on the API reference page, https://developers.google.com/youtube/iframe_api_reference. I call event.target.loadModule('captions'); on a player state change event.

I could not find any documentation on their API reference page about loading caption modules, but I did find the code to load the caption module in this ticket https://code.google.com/p/gdata-issues/issues/detail?id=444

I tried this on both Safari and Chrome with no luck.

The local storage values I see when youtube is opened is:

yt-remote-connected-devices {"data":"[]","expiration":1412291704974,"creation":1412205304974}
yt-remote-device-id {"data":"27238aac-9452-4ae8-9b9f-1e29278e4d3b","expiration":1443741293991,"creation":1412205293992}
yt-remote-load-account-screens  {"data":"false","expiration":1443741304972,"creation":1412205304972}
yt-remote-online-screens    {"data":"[]","expiration":1412205364973,"creation":1412205304973}

Notice that its missing entries for captions (I can't recall what the key values were, but I know there were two of them. One to enable captions and the other for caption settings).

The demo page (https://developers.google.com/youtube/youtube_player_demo) shows closed captions if you check cc_load_policy, but that's only an AS3 option.

This was working on October 6th, but I cannot get it to work anymore. I'm not sure if the youtube script itself changed.

I tried moving event.target.loadModule('captions'); to the onPlayerReady handler, but that didn't work either.

Any help would be greatly appreciated.

Swanhildas answered 8/10, 2014 at 17:16 Comment(0)
S
0

I was able to load captions by setting cc_load_policy to 1. I also realized that the sample video on the youtube site defaulting to using <embed> (read: Flash), instead of using the html5 video player.

I am not certain when the iframe player decides to use over , but I did find a video that does use the html5 video tag. Even so, I had to set cc_load_policy to 1 in order for it to work. Neither loadModule nor unloadModule works anymore.

I hope this helps anyone else who ran into this issue.

Swanhildas answered 13/10, 2014 at 21:49 Comment(4)
I am pulling my hair out trying to get this to work. The API was updated on 10/14, but calling cc_load_policy in my script isn't loading the captions. Did you have to change your HTML markup at all?Jennyjeno
I didn't change the HTML markup. What does your code look like?Swanhildas
I figured out that it only works when captions have been uploaded to the video, not with the YouTube-generated captions. The docs don't make any mention of that, though.Jennyjeno
@BrianBennett were you able to figure out a way to programmatically display the auto-generated captions?Natatorial

© 2022 - 2024 — McMap. All rights reserved.