Skip to content

Instantly share code, notes, and snippets.

@misteroneill
Last active January 11, 2017 16:38
Show Gist options
  • Save misteroneill/c239898a9bab74abf59998403e247436 to your computer and use it in GitHub Desktop.
Save misteroneill/c239898a9bab74abf59998403e247436 to your computer and use it in GitHub Desktop.
Video.js 6 plugin examples.
const Plugin = videojs.getPlugin('plugin');
class Advanced extends Plugin {
constructor(player, options) {
super(player, options);
// Whenever the player emits a playing or paused event, we update the
// state if necessary.
this.on(player, ['playing', 'paused'], this.updateState);
this.on('statechanged', this.logState);
}
dispose() {
super.dispose();
videojs.log('the advanced plugin is being disposed');
}
updateState() {
this.setState({playing: !player.paused()});
}
logState(changed) {
videojs.log(`the player is now ${this.state.playing ? 'playing' : 'paused'}`);
}
}
videojs.registerPlugin('advanced', Advanced);
const player = videojs('example-player');
player.advanced();
// This will begin playback, which will trigger a "playing" event, which will
// update the state of the plugin, which will cause a message to be logged.
player.play();
// This will pause playback, which will trigger a "paused" event, which will
// update the state of the plugin, which will cause a message to be logged.
player.pause();
// Dispose the plugin, but don't otherwise change the player.
player.advanced().dispose();
// This will begin playback, but the plugin has been disposed, so it will not
// log any messages.
player.play();
function basic(options) {
if (options.customClass) {
this.addClass(options.customClass);
}
this.on('playing', function() {
videojs.log('playback began!');
});
}
videojs.registerPlugin('basic', basic);
const player = videojs('example-player');
player.basic({customClass: 'hello-world'});
// Once the player begins playing, the "playback began!" message will be logged.
player.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment