javascriptdynamicvideo.jscaptions

Dynamically change videojs captions with addTextTrack()


I'm trying to build some thing like a video gallery which you can select a video to show up by clicking on its thumbnail. Now I'm at phase of loading appropriate subtitles for the chosen video. Thanks to google I understand that videojs has a method to help me called addTextTrack() but unfortunately there is not a good sample or documentation for it. After all I tried to find its parameters and behavior via reading the video.dev.js codes. but as I understand this method has just three params (kind, label, language) and the thing that I didn't understand is that: How can I set the src to load the subtitle file. I think its a bug and it doesn't work properly and I want to report it if you're agree with me.

The following code adds cc icon to the player but it doesn't show the subtitle (How can it be shown when I didn't tell him the URL to load)

var myPlayer = videojs('video-id');
myPlayer.addTextTrack('captions', 'En', 'English');

I checked videojs 5.0.0 addTextTrack method and there wasn't any significant changes.


Solution

  • After about a month without any answer to my question, I don't know yet why addTextTrack() doesn't work properly. But thanks God, I found a way to achieve my goal:

    To dynamically changing all text tracks

    var oldTracks = player.remoteTextTracks();
    var i = oldTracks.length;
    while (i--) {
      player.removeRemoteTextTrack(oldTracks[i]);
    }
    myNewTracks.forEach(function(track) {
      player.addRemoteTextTrack(track);
    });