用于HTML5的Youtube隐藏式标题不起作用

时间:2014-10-08 17:16:27

标签: youtube youtube-iframe-api

我在使用iframe播放器在youtube视频上显示隐藏字幕时遇到问题。

这是我正在使用的代码:

<!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>

这里是一个要点https://gist.github.com/khirakawa/0a81b3039a85b9875b59

这基本上与API参考页https://developers.google.com/youtube/iframe_api_reference上的示例代码相同。我在播放器状态更改事件上调用event.target.loadModule('captions');

我在他们的API参考页面上找不到有关加载标题模块的任何文档,但我确实找到了在此票证中加载标题模块的代码https://code.google.com/p/gdata-issues/issues/detail?id=444

我在Safari和Chrome上都尝试了这个,没有运气。

我打开youtube时看到的本地存储值是:

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}

请注意其缺少的字幕条目(我无法回想起关键值是什么,但我知道其中有两个。一个用于启用字幕,另一个用于字幕设置)。

如果您检查cc_load_policy,那么演示页面(https://developers.google.com/youtube/youtube_player_demo)会显示隐藏式字幕,但这只是一个AS3选项。

这是在10月6日工作,但我不能让它继续工作。我不确定youtube脚本本身是否发生了变化。

我尝试将event.target.loadModule('captions');移至onPlayerReady处理程序,但这也不起作用。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我可以通过将cc_load_policy设置为1来加载字幕。我还意识到youtube网站上的示例视频默认使用<embed>(读取:Flash),而不是使用html5视频播放器。

我不确定iframe播放器何时决定使用,但我确实找到了一个使用html5视频标签的视频。即使这样,我也必须将cc_load_policy设置为1才能使其正常工作。 loadModuleunloadModule都不再适用。

我希望这可以帮助遇到此问题的其他人。