// Resolution switching support for videojs // // In this plugin I'm really going out of my way to *not* override the // core videojs namespace and to *not* change the core API. As a // result this plugin is not as efficient as it might be. It // initializes itself *for each player* as scoped variables inside the // plugin closure and grafts itself on to *the instance on which it was // called* rather than on the videojs player prototype. I don't expect // this to be a big deal for anybody. videojs.plugin('resolutions', function(options) { var player = this; // 'reduce' utility method // @param {Array} array to iterate over // @param {Function} iterator function for collector // @param {Array|Object|Number|String} initial collector // @return collector vjs.reduce = function(arr, fn, init, n) { if (!arr || arr.length === 0) { return; } for (var i=0,j=arr.length; i maxRes ? maxRes : preferredRes; return typeSources[actualRes]; } }; // convenience method // @return {String} cached resolution label: // "SD" player.resolution = function(){ return this.cache_.src.res; }; // takes a source and switches the player's stream to it on the fly // @param {Object} singular source: // { // "data-default": "true", // "data-res": "SD", // "type": "video/mp4", // "src": "http://some_video_url_sd" // } player.changeResolution = function(new_source){ // has the exact same source been chosen? if (this.cache_.src === new_source.src){ this.trigger('resolutionchange'); return this; // basically a no-op } // remember our position and playback state var curTime = this.currentTime(); var remainPaused = this.paused(); // pause playback this.pause(); // attempts to stop the download of the existing video this.resolutions_.stopStream(); // HTML5 tends to not recover from reloading the tech but it can // generally handle changing src. Flash generally cannot handle // changing src but can reload its tech. if (this.techName === "Html5"){ this.src(new_source.src); } else { this.loadTech(this.techName, {src: new_source.src}); } // when the technology is re-started, kick off the new stream this.ready(function() { this.one('loadeddata', vjs.bind(this, function() { this.currentTime(curTime); })); this.trigger('resolutionchange'); if (!remainPaused) { this.load(); this.play(); } // remember this selection vjs.setLocalStorage('videojs_preferred_res', parseInt(new_source.index, 10)); }); }; /* Resolution Menu Items ================================================================================ */ var ResolutionMenuItem = videojs.MenuItem.extend({ init: function(player, options){ // Modify options for parent MenuItem class's init. options['label'] = options.source['data-res']; videojs.MenuItem.call(this, player, options); this.source = options.source; this.resolution = options.source['data-res']; this.player_.one('loadstart', vjs.bind(this, this.update)); this.player_.on('resolutionchange', vjs.bind(this, this.update)); } }); ResolutionMenuItem.prototype.onClick = function(){ videojs.MenuItem.prototype.onClick.call(this); this.player_.changeResolution(this.source); }; ResolutionMenuItem.prototype.update = function(){ var player = this.player_; if ((player.cache_['src'] === this.source.src)) { this.selected(true); } else { this.selected(false); } }; /* Resolutions Button ================================================================================ */ var ResolutionButton = videojs.MenuButton.extend({ init: function(player, options) { videojs.MenuButton.call(this, player, options); if (this.items.length <= 1) { this.hide(); } } }); ResolutionButton.prototype.sourceResolutions_; ResolutionButton.prototype.sourceResolutions = function() { return this.sourceResolutions_; }; ResolutionButton.prototype.onClick = function(e){ // Only proceed if the target of the click was a DIV (just the button and its inner div, not the menu) // This prevents the menu from opening and closing when one of the menu items is clicked. if (e.target.className.match(/vjs-control-content/)) { // Toggle the 'touched' class this[this.el_.className.match(/touched/) ? "removeClass" : "addClass"]("touched"); } else { // Remove the 'touched' class from all control bar buttons with menus to hide any already visible... var buttons = document.getElementsByClassName('vjs-menu-button'); for(var i=0;i