Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 532
secretsuny Avatar asked Nov 30 '22 04:11

secretsuny


2 Answers

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
like image 116
James Irwin Avatar answered Dec 10 '22 02:12

James Irwin


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.

like image 20
user3193413 Avatar answered Dec 10 '22 03:12

user3193413