Skip to content

Instantly share code, notes, and snippets.

@robwalch
Last active April 25, 2017 20:38
Show Gist options
  • Save robwalch/6ad03affe69eaed2c322 to your computer and use it in GitHub Desktop.
Save robwalch/6ad03affe69eaed2c322 to your computer and use it in GitHub Desktop.
JW Player with Backbone Events
// Extend JW Player instance with Backbone Events
// binds jw events to trigger using lowercase names 'ready', 'play', 'time', etc...
// allows us to add and remove listers using 'on', 'off', 'once', etc...
// Requires jwplayer.js and Backbone.js (which includes underscore)
// For more info see: http://backbonejs.org/#Events
// and http://support.jwplayer.com/customer/portal/articles/1413089-javascript-api-reference
// IMPORTANT: define listeners in the same scope as the new jw instance returned by setup
var jwEventListeners = {};
_.each([
'Ready', 'SetupError',
'Playlist', 'PlaylistItem', 'PlaylistComplete',
'BeforePlay', 'BeforeComplete',
'QualityLevels', 'QualityChange',
'CaptionsList', 'CaptionsChange',
'Buffer', 'Play', 'Pause', 'Idle',
'BufferChange', 'BufferFull',
'Meta', 'Time', 'Seek', 'Complete', 'Error',
'Mute', 'Volume',
'Controls', 'Cast', 'DisplayClick', 'Fullscreen', 'Resize',
'AdClick', 'AdCompanions', 'AdImpression', 'AdSkipped',
'AdPause', 'AdPlay', 'AdMeta', 'AdTime', 'AdComplete', 'AdError'
], function(onName) {
var name = onName.toLowerCase();
jwEventListeners['on' + onName] = function(e) {
jw.trigger(name, e);
};
});
// get new player instance
var jw = jwplayer('jwplayer').setup({
events: jwEventListeners
/* addition player config options goes here
see: http://support.jwplayer.com/customer/portal/articles/1413113-configuration-options-reference
*/
});
// add the on, off, once methods to our new player:
_.extend(jw, Backbone.Events);
// now you can do things like...
jw.once('play' function(e) {
// respond to only the first play event
console.log('playback started', e);
}).on('time', function(e) {
// stop responding to time events after 1 second
if (e.position > 1) {
this.off('time');
}
});
@donato
Copy link

donato commented Sep 16, 2014

The full list of event functions is
– on
– off
– trigger
– once
– listenTo
– stopListening
– listenToOnce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment