Youtube API - how to use custom controls to turn captions on/off, change language?
Asked Answered
D

3

5

I have a youtube player with custom HTML controls and would like to add a button to turn on/off closed caption and to toggle the language. Anyone have experience with using either the JS or iframe API to do this? I know that appending cc_load_policy=1 to the url will force the captions to display, but I am using custom controls and would like to add a toggle button. I tried using the loadVideoByUrl method and appending the cc parameter, but that doesn't work doesn't seem to work.

I was also only able to get the player to show captions in the user's default language with the iframe method, not the swf object method. Swf object always defaults to English. If anyone has any insights to this as well, please let me know!

Daze answered 6/12, 2012 at 1:58 Comment(0)
P
1

Unfortunately, we don't have full API support over captions right now. You can add yourself to this external feature request to be notified when this changes: https://code.google.com/p/gdata-issues/issues/detail?id=444

(Despite the age of that feature request, there are people actively looking on adding support.)

Paynter answered 6/12, 2012 at 20:27 Comment(0)
E
21

I have not found this anywhere in their api docs, but with your youtube player object you should be able to do:

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
Epicure answered 29/3, 2014 at 1:13 Comment(5)
Worked like a charm for me.Caledonian
if that doesn't work, I suggest to call those methods inside a custom onStateChange event. I had to force disable the captions and using unloadModule() inside that event worked for me, after attempting to call it somewhere else (like inside onReady)Octavla
for those wondering, it does work but not for auto-generated subs. best to call it AFTER the video has started playing. tracks = ytPlayer.getOption('captions', 'tracklist'); can get you a list of proper captions, but not auto-generated captionsCarmellacarmelle
This should be the accepted answer! It wasn't working when using on onReady() or onApiChange(), but when I moved it to onStateChange() as mentioned before it finally worked.Protestantism
I think it's important to note that the last section to SetOption will Not work unless you load those modules right from the get go. I was attempting to leave captions off until I was ready to use them and the setOption call was irrelevant until that was in place.Sinistrality
P
1

Unfortunately, we don't have full API support over captions right now. You can add yourself to this external feature request to be notified when this changes: https://code.google.com/p/gdata-issues/issues/detail?id=444

(Despite the age of that feature request, there are people actively looking on adding support.)

Paynter answered 6/12, 2012 at 20:27 Comment(0)
R
1

function onPlayerStateChange(event) 
{	
  try
  {
    player.unloadModule("captions");  
    player.unloadModule("cc"); 
  }
  catch (exception)
  {
    LoggerUtil.logError("Error when trying to unloadModule youtube captions: " + exception);
  }
  // The rest of your function
}

Thanks James Irwin those two lines worked for me when testing on android 4.4.2 and android 6.0.1

player.unloadModule("captions");
player.unloadModule("cc");

You should put this two lines inside the onPlayerStateChange function.

Raffinate answered 13/7, 2016 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.