检测嵌入式Youtube播放器标题

时间:2012-08-17 18:45:16

标签: javascript youtube youtube-api

是否可以检测嵌入式播放器中的youtube字幕是打开还是关闭?

如果用户选择显示字幕,目前无法抑制字幕。如果标题已开启,我想显示自定义消息。

documentation中,据我所知,没有提及这一点。

2 个答案:

答案 0 :(得分:5)

我没有在他们的api文档中找到它,但是使用你的youtube播放器对象你应该可以做到:

player.getOptions("captions") || player.getOptions("cc")  //detects if captions were ever loaded at one point.

你也可以通过js打开它:

player.loadModule("captions");  //Works for html5 ignored by AS3
player.loadModule("cc");  //Works for AS3 ignored by html5

将其关闭:

player.unloadModule("captions");  //Works for html5 ignored by AS3
player.unloadModule("cc");  //Works for AS3 ignored by html5

如果加载了模块,则更改哪种语言:

player.setOption("captions", "track", {"languageCode": "es"});  //Works for html5 ignored by AS3
player.setOption("cc", "track", {"languageCode": "es"});  //Works for AS3 ignored by html5

答案 1 :(得分:4)

很容易,你可以;)

<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
  <div id="ytapiplayer">
    You need Flash player 8+ and JavaScript enabled to view this video.
  </div>

  <script type="text/javascript">

    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/QRS8MkLhQmM?enablejsapi=1&playerapiid=ytplayer&version=3",
                       "ytapiplayer", "640", "480", "8", null, null, params, atts);

    var stateChange = function(a) {
            if (ytplayer.getOptions().indexOf("cc") !== -1) {
        alert("closed captions are on");
      } else {
        alert("closed captions are off");
      }
    }
   function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("myytplayer");
      ytplayer.addEventListener("onStateChange","stateChange");
    }
  </script></body>
</html>

BTW:谷歌摇滚;)

这应该可以解决问题 - 请不要在生产模式下使用它 - 这只是一个快速的总结。你需要倾听你想要的状态,我只是警告任何事件;)