是否可以检测嵌入式播放器中的YouTube字幕是打开还是关闭?
如果用户选择显示字幕,则目前无法禁止显示字幕。如果字幕打开,我想显示自定义消息。
据我所知,在文档中没有提到这一点。
我在他们的 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
像馅饼一样简单,你可以;)
<!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>
顺便说一句:谷歌摇滚;)
这应该可以解决问题 - 请不要在生产模式下使用它 - 这只是一个快速总结。你需要倾听你想要的状态,我只是在任何事件中提醒;)