Pass subtitle selection on to Chromecast on Web
Currently, it is intended behavior that when you start casting the player to a Chromecast device the subtitle selection setting isn't carried over. A new player is generated with the SourceConfiguration of the sender, but not any adjustments.
You can work around the default behavior by adding event listeners to the player.
The code adds an event on textTracks change. Whenever the subtitles change, this event is called and stores the current active textTrack in currentTextTrack by iterating over the textTracks array and checking whether .mode is showing. Once a Chromecast session is created, the code will check if the ID of the textTracks in the new player corresponds with the ID of the stored currentTextTrack. If so, it will activate that textTrack.
Solution
Here is how you pass the current active subtitle track to the Chromecast session:
var currentTextTrack;
player.textTracks.addEventListener('change', function () {
for (var t in player.textTracks) {
if (player.textTracks[t].mode === 'showing') {
currentTextTrack = player.textTracks[t];
}
}
});
player.cast.chromecast.addEventListener('statechange', (event) => {
if (event.state === 'connected') {
player.addEventListener('playing', function () {
for (var tt in player.textTracks) {
if (player.textTracks[tt].id === currentTextTrack.id) {
player.textTracks[tt].mode = 'showing';
}
}
});
}
});