Is it possible to detect whether youtube captions are on or off in an embedded player?
There is currently no way to suppress the captions if users choses to display them. I would like to show a customized message if the captions are on.
In the documentation there is no mention of this as far as I can say.
Is it possible to detect whether youtube captions are on or off in an embedded player?
There is currently no way to suppress the captions if users choses to display them. I would like to show a customized message if the captions are on.
In the documentation there is no mention of this as far as I can say.
Share Improve this question edited Aug 17, 2012 at 21:45 tinman 6,6081 gold badge34 silver badges47 bronze badges asked Aug 17, 2012 at 18:45 Mikulas DiteMikulas Dite 7,9419 gold badges61 silver badges103 bronze badges 5- check this page maybe it helps developers.google./youtube/2.0/… – unloco Commented Aug 17, 2012 at 19:12
- @UnLoCo that relates to the video itself, what I seek is in no way dependent on the video but rather on the embedded player. – Mikulas Dite Commented Aug 17, 2012 at 22:59
- Seems it is not possible with conventional means. Could it be done via checking dom of html5 player? The captions should be there, right? – Mikulas Dite Commented Aug 19, 2012 at 18:20
- @MikulasDite HTML5 player will be embedded in an iframe, so you won't be able to access its content [same origin policy] – Hrant Khachatrian Commented Aug 22, 2012 at 12:21
- @MikulasDite: You would have to use postMessage to municate with the iframe of the HTML5 player (direct access won't be possible due to same-origin policy), the API exposed there is the same as for the Flash player. – Wladimir Palant Commented Aug 27, 2012 at 9:24
2 Answers
Reset to default 6I have not found this anywhere in their api docs, but with your youtube player object you should be able to do:
player.getOptions("captions") || player.getOptions("cc") //detects if captions were ever loaded at one point.
You can also turn it on via js by:
player.loadModule("captions"); //Works for html5 ignored by AS3
player.loadModule("cc"); //Works for AS3 ignored by html5
to turn it off:
player.unloadModule("captions"); //Works for html5 ignored by AS3
player.unloadModule("cc"); //Works for AS3 ignored by html5
to change which language if the module is loaded:
player.setOption("captions", "track", {"languageCode": "es"}); //Works for html5 ignored by AS3
player.setOption("cc", "track", {"languageCode": "es"}); //Works for AS3 ignored by html5
Easy as pie, you can ;)
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<script src="//ajax.googleapis./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./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: Google rocks ;)
this should do the trick - please don't use it in production mode - it was just a quick sum-up. You need to listen to the state you want, I'm just alerting at any event ;)