适用于 HTML5 的 YouTube 隐藏式字幕不起作用 [英] Youtube Closed Caption for HTML5 Not Working

查看:30
本文介绍了适用于 HTML5 的 YouTube 隐藏式字幕不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我正在使用的代码:

<身体><!-- 1. <iframe>(和视频播放器)将替换这个 <div>标签.--><div id="玩家"></div><脚本>//2. 此代码异步加载 IFrame Player API 代码.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. 这个函数创建一个<iframe>(和 YouTube 播放器)//API 代码下载后.var 播放器;函数 onYouTubeIframeAPIReady() {player = new YT.Player('玩家', {高度:'390',宽度:'640',videoId: 'M7lc1UVf-VE',事件:{'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}//4. 当视频播放器准备就绪时,API 将调用此函数.函数 onPlayerReady(event) {event.target.playVideo();}//5. API 在玩家状态改变时调用该函数.//该函数表示在播放视频时(state=1),//玩家应该播放六秒钟然后停止.函数 onPlayerStateChange(event) {event.target.loadModule('captions');}</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}

请注意,它缺少字幕条目(我不记得键值是什么,但我知道有两个.一个用于启用字幕,另一个用于字幕设置).

演示页面(https://developers.google.com/youtube/youtube_player_demo)如果您选中 cc_load_policy,则会显示隐藏式字幕,但这只是 AS3 选项.

这在 10 月 6 日有效,但我无法再让它工作了.我不确定 youtube 脚本本身是否发生了变化.

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

任何帮助将不胜感激.

解决方案

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

我不确定 iframe 播放器何时决定使用 over ,但我确实找到了一个使用 html5 video 标签的视频.即便如此,我还是必须将 cc_load_policy 设置为 1 才能使其工作.loadModuleunloadModule 都不再起作用.

我希望这对遇到此问题的其他人有所帮助.

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.

解决方案

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.

这篇关于适用于 HTML5 的 YouTube 隐藏式字幕不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆