Skip to content

Instantly share code, notes, and snippets.

@brianpkelley
Created July 18, 2014 15:48
Show Gist options
  • Save brianpkelley/bf080f21a06b43bcb74b to your computer and use it in GitHub Desktop.
Save brianpkelley/bf080f21a06b43bcb74b to your computer and use it in GitHub Desktop.
Unloading and loading of Video.js text tracks while updating their menuButton
(function() {
'use strict';
videojs.Player.prototype.unloadTextTracks = function( kind ) {
var kinds = ['captions','subtitles','chapters','descriptions','metadata'];
var i;
var tracks = this.textTracks_ = this.textTracks_ || [];
// If no specific type is specified then clear them all
if ( !kind ) {
for ( i = 0; i < kinds.length; i++ ) { // clear them all
this.unloadTextTracks( kinds[i] );
}
return;
}
// Remove them from their respective buttons
this.clearTrackButton( kind );
// Remove them from the tracks array.
for ( i = tracks.length-1; i >= 0; i-- ) {
if ( tracks[i] && tracks[i].options_.kind === kind ) {
tracks[i].deactivate();
tracks[i].dispose();
tracks.splice(i,1);
}
}
};
videojs.Player.prototype.clearTrackButton = function( kind ) {
var controlBar = this.controlBar;
if ( controlBar[kind + 'Button'] ) {
// Remove all children (menus);
while( controlBar[kind + 'Button'].children_.length ) {
controlBar[kind + 'Button'].children_[0].dispose();
}
controlBar[kind + 'Button'].hide();
}
};
var addTextTrackSuper_ = videojs.Player.prototype.addTextTrack;
videojs.Player.prototype.addTextTrack = function(kind, label, language, options) {
// Call the original addTextTrack function.
addTextTrackSuper_.apply(this, arguments);
// Clear the menu
this.clearTrackButton( kind );
// Re-create the menu and show it
var button = this.controlBar[kind + 'Button'];
button.menu = button.createMenu();
button.addChild( button.menu );
button.show();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment