javascripthtmlhtml5-videomedia-sourcempeg-dash

Delete a TextTrack from a video


Is there a good way to delete a single TextTrack added via JavaScript to a HTML5 <video> tag? The following code is a simple demo of how to add a track, but I haven't found a good way to remove one:

document.querySelector('video#myVideo').addTextTrack(...);

Solution

  • There is no mechanism to remove an added text-track (probably a flaw (?) in the current specification. There is also an onremovetrack event but...).

    The only thing we can do is to disable or hide it. Make sure you keep a reference to the track:

    var video = document.querySelector('video#myVideo');
    var track = video.addTextTrack("...");
    ...
    track.mode = "showing";  // when cues are added and is ready
    

    Then when you no longer need it:

    track.mode = "disabled"; // or "hidden"
    

    (you might get around resetting the src as well, but I haven't tried this..)