Youtube Closed Caption for HTML5 Not Working

拟墨画扇 提交于 2019-12-04 05:57:00

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/26262666/youtube-closed-caption-for-html5-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!