Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed Caption Button and Dynamic Language Change for Captions #1175

Open
HamzaIsrar12 opened this issue Oct 20, 2024 · 2 comments
Open

Closed Caption Button and Dynamic Language Change for Captions #1175

HamzaIsrar12 opened this issue Oct 20, 2024 · 2 comments
Labels

Comments

@HamzaIsrar12
Copy link

HamzaIsrar12 commented Oct 20, 2024

Hi Pier,

I would like to add a closed caption button to the YouTube player to enable/disable captions dynamically. Additionally, I want to allow users to select the available languages for captions.

I already have access to the available languages through an internal API, but I'm facing an issue with changing the caption language or enabling/disabling captions after the player is initialized.

Issue/Blocker:

  • It seems that changing the caption language or toggling captions is only possible through the IFramePlayerOptions, which can only be set during the player's initialization.
  • The library does not support reinitializing the player after it’s been created.
  • Destroying and recreating the player to apply new IFramePlayerOptions results in a glitchy experience.

Question: Is there a way to dynamically update the caption settings (enable/disable and change languages) without destroying and recreating the player? Or, is there a recommended solution to handle this more smoothly?

Any insights or workarounds would be greatly appreciated! Thank you.

@HamzaIsrar12 HamzaIsrar12 changed the title Feature Request: Closed Caption Button and Dynamic Language Change for Captions Closed Caption Button and Dynamic Language Change for Captions Oct 20, 2024
@PierfrancescoSoffritti
Copy link
Owner

I suggest to

  1. finding a way to do this with the IFrame Player.
  2. try to replicate that in the library.

If 1 is possible but 2 is not, we can work on a solution to enable it.

@Serkali-sudo
Copy link
Contributor

You can get a list of available captions and select any of them without recreating the player.
I dont know why they didnt mention this in the documentation but there are options called tracklist and track.

First of all set cc_load_policy to 1 in iframe player settings.

You should use codes below inside onApiChange because onApiChange event fires when captions or other modules loaded.

You can get available captions like this:

const tracks = player.getOption('captions', 'tracklist');

it returns a json array like this:

[
  {
    "languageCode": "en",
    "languageName": "English",
    "displayName": "English",
    "kind": "",
    "name": "",
    "id": null,
    "is_servable": false,
    "is_default": false,
    "is_translateable": true,
    "vss_id": ".en"
  },
  {
    "languageCode": "fr",
    "languageName": "French",
    "displayName": "French",
    "kind": "",
    "name": "",
    "id": null,
    "is_servable": false,
    "is_default": false,
    "is_translateable": true,
    "vss_id": ".fr"
  }
]

Now that you have a list of available captions you can choose them with this:

player.setOption('captions', 'track', /*put selected json object from the list here*/);

To turn off captions programatically you can use this:

player.setOption('captions', 'track', {});

Obviously, to implement these changes you are gonna have to edit the library.
Implementing neccesary javascript interfaces to control the player from java or kotlin is for you to figure out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants