mirror of
https://github.com/kmoskwiak/videojs-resolution-switcher.git
synced 2025-10-03 09:49:21 +02:00
custom source selecting function in options
This commit is contained in:
parent
8d943b4a98
commit
9ba742ca0e
1 changed files with 31 additions and 26 deletions
|
@ -15,13 +15,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
(function(window, videojs) {
|
(function(window, videojs) {
|
||||||
|
|
||||||
|
|
||||||
var defaults = {},
|
var defaults = {},
|
||||||
videoJsResolutionSwitcher,
|
videoJsResolutionSwitcher,
|
||||||
currentResolution = {}, // stores current resolution
|
currentResolution = {}, // stores current resolution
|
||||||
menuItemsHolder = {}; // stores menuItems
|
menuItemsHolder = {}; // stores menuItems
|
||||||
|
|
||||||
function setSourcesSanitized(player, sources, label, customSourcePicker) {
|
function setSourcesSanitized(player, sources, label, customSourcePicker) {
|
||||||
currentResolution = {
|
currentResolution = {
|
||||||
label: label,
|
label: label,
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
return {src: src.src, type: src.type, res: src.res};
|
return {src: src.src, type: src.type, res: src.res};
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Resolution menu item
|
* Resolution menu item
|
||||||
*/
|
*/
|
||||||
|
@ -71,6 +71,10 @@
|
||||||
if(!isPaused){
|
if(!isPaused){
|
||||||
this.player_.bigPlayButton.hide();
|
this.player_.bigPlayButton.hide();
|
||||||
}
|
}
|
||||||
|
if(typeof customSourcePicker !== 'function' &&
|
||||||
|
typeof this.options_.customSourcePicker === 'function'){
|
||||||
|
customSourcePicker = this.options_.customSourcePicker;
|
||||||
|
}
|
||||||
// Change player source and wait for loadeddata event, then play video
|
// Change player source and wait for loadeddata event, then play video
|
||||||
// loadedmetadata doesn't work right now for flash.
|
// loadedmetadata doesn't work right now for flash.
|
||||||
// Probably because of https://github.com/videojs/video-js-swf/issues/124
|
// Probably because of https://github.com/videojs/video-js-swf/issues/124
|
||||||
|
@ -85,8 +89,8 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Resolution menu button
|
* Resolution menu button
|
||||||
*/
|
*/
|
||||||
|
@ -97,9 +101,9 @@
|
||||||
this.label = label;
|
this.label = label;
|
||||||
this.label.innerHTML = options.initialySelectedLabel;
|
this.label.innerHTML = options.initialySelectedLabel;
|
||||||
// Sets this.player_, this.options_ and initializes the component
|
// Sets this.player_, this.options_ and initializes the component
|
||||||
MenuButton.call(this, player, options);
|
MenuButton.call(this, player, options, settings);
|
||||||
this.controlText('Quality');
|
this.controlText('Quality');
|
||||||
|
|
||||||
if(settings.dynamicLabel){
|
if(settings.dynamicLabel){
|
||||||
this.el().appendChild(label);
|
this.el().appendChild(label);
|
||||||
}else{
|
}else{
|
||||||
|
@ -116,7 +120,7 @@
|
||||||
item.selected(item === clickedItem);
|
item.selected(item === clickedItem);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
for (var key in labels) {
|
for (var key in labels) {
|
||||||
if (labels.hasOwnProperty(key)) {
|
if (labels.hasOwnProperty(key)) {
|
||||||
menuItems.push(new ResolutionMenuItem(
|
menuItems.push(new ResolutionMenuItem(
|
||||||
|
@ -124,7 +128,8 @@
|
||||||
{
|
{
|
||||||
label: key,
|
label: key,
|
||||||
src: labels[key],
|
src: labels[key],
|
||||||
initialySelected: key === this.options_.initialySelectedLabel
|
initialySelected: key === this.options_.initialySelectedLabel,
|
||||||
|
customSourcePicker: this.options_.customSourcePicker
|
||||||
},
|
},
|
||||||
onClickUnselectOthers,
|
onClickUnselectOthers,
|
||||||
this.label));
|
this.label));
|
||||||
|
@ -135,7 +140,7 @@
|
||||||
return menuItems;
|
return menuItems;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the plugin.
|
* Initialize the plugin.
|
||||||
* @param {object} [options] configuration for the plugin
|
* @param {object} [options] configuration for the plugin
|
||||||
|
@ -144,9 +149,9 @@
|
||||||
var settings = videojs.mergeOptions(defaults, options),
|
var settings = videojs.mergeOptions(defaults, options),
|
||||||
player = this,
|
player = this,
|
||||||
label = document.createElement('span');
|
label = document.createElement('span');
|
||||||
|
|
||||||
label.classList.add('vjs-resolution-button-label');
|
label.classList.add('vjs-resolution-button-label');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates player sources or returns current source URL
|
* Updates player sources or returns current source URL
|
||||||
* @param {Array} [src] array of sources [{src: '', type: '', label: '', res: ''}]
|
* @param {Array} [src] array of sources [{src: '', type: '', label: '', res: ''}]
|
||||||
|
@ -164,12 +169,12 @@
|
||||||
src = src.sort(compareResolutions);
|
src = src.sort(compareResolutions);
|
||||||
var groupedSrc = bucketSources(src);
|
var groupedSrc = bucketSources(src);
|
||||||
var choosen = chooseSrc(groupedSrc, src);
|
var choosen = chooseSrc(groupedSrc, src);
|
||||||
var menuButton = new ResolutionMenuButton(player, { sources: groupedSrc, initialySelectedLabel: choosen.label , initialySelectedRes: choosen.res }, settings, label);
|
var menuButton = new ResolutionMenuButton(player, { sources: groupedSrc, initialySelectedLabel: choosen.label , initialySelectedRes: choosen.res , customSourcePicker: settings.cusotomSourcPicker}, settings, label);
|
||||||
menuButton.el().classList.add('vjs-resolution-button');
|
menuButton.el().classList.add('vjs-resolution-button');
|
||||||
player.controlBar.resolutionSwitcher = player.controlBar.addChild(menuButton);
|
player.controlBar.resolutionSwitcher = player.controlBar.addChild(menuButton);
|
||||||
return setSourcesSanitized(player, choosen.sources, choosen.label);
|
return setSourcesSanitized(player, choosen.sources, choosen.label);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current resolution or sets one when label is specified
|
* Returns current resolution or sets one when label is specified
|
||||||
* @param {String} [label] label name
|
* @param {String} [label] label name
|
||||||
|
@ -183,7 +188,7 @@
|
||||||
}
|
}
|
||||||
return player;
|
return player;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method used for sorting list of sources
|
* Method used for sorting list of sources
|
||||||
* @param {Object} a - source object with res property
|
* @param {Object} a - source object with res property
|
||||||
|
@ -194,7 +199,7 @@
|
||||||
if(!a.res || !b.res){ return 0; }
|
if(!a.res || !b.res){ return 0; }
|
||||||
return (+b.res)-(+a.res);
|
return (+b.res)-(+a.res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Group sources by label, resolution and type
|
* Group sources by label, resolution and type
|
||||||
* @param {Array} src Array of sources
|
* @param {Array} src Array of sources
|
||||||
|
@ -210,24 +215,24 @@
|
||||||
initResolutionKey(resolutions, 'label', source);
|
initResolutionKey(resolutions, 'label', source);
|
||||||
initResolutionKey(resolutions, 'res', source);
|
initResolutionKey(resolutions, 'res', source);
|
||||||
initResolutionKey(resolutions, 'type', source);
|
initResolutionKey(resolutions, 'type', source);
|
||||||
|
|
||||||
appendSourceToKey(resolutions, 'label', source);
|
appendSourceToKey(resolutions, 'label', source);
|
||||||
appendSourceToKey(resolutions, 'res', source);
|
appendSourceToKey(resolutions, 'res', source);
|
||||||
appendSourceToKey(resolutions, 'type', source);
|
appendSourceToKey(resolutions, 'type', source);
|
||||||
});
|
});
|
||||||
return resolutions;
|
return resolutions;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initResolutionKey(resolutions, key, source) {
|
function initResolutionKey(resolutions, key, source) {
|
||||||
if(resolutions[key][source[key]] == null) {
|
if(resolutions[key][source[key]] == null) {
|
||||||
resolutions[key][source[key]] = [];
|
resolutions[key][source[key]] = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendSourceToKey(resolutions, key, source) {
|
function appendSourceToKey(resolutions, key, source) {
|
||||||
resolutions[key][source[key]].push(source);
|
resolutions[key][source[key]].push(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Choose src if option.default is specified
|
* Choose src if option.default is specified
|
||||||
* @param {Object} groupedSrc {res: { key: [] }}
|
* @param {Object} groupedSrc {res: { key: [] }}
|
||||||
|
@ -247,13 +252,13 @@
|
||||||
} else if (groupedSrc.res[selectedRes]) {
|
} else if (groupedSrc.res[selectedRes]) {
|
||||||
selectedLabel = groupedSrc.res[selectedRes][0].label;
|
selectedLabel = groupedSrc.res[selectedRes][0].label;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(selectedRes === undefined){
|
if(selectedRes === undefined){
|
||||||
return {res: selectedRes, label: selectedLabel, sources: groupedSrc.label[selectedLabel]};
|
return {res: selectedRes, label: selectedLabel, sources: groupedSrc.label[selectedLabel]};
|
||||||
}
|
}
|
||||||
return {res: selectedRes, label: selectedLabel, sources: groupedSrc.res[selectedRes]};
|
return {res: selectedRes, label: selectedLabel, sources: groupedSrc.res[selectedRes]};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create resolution switcher for videos form <source> tag inside <video>
|
// Create resolution switcher for videos form <source> tag inside <video>
|
||||||
if(player.options_.sources.length > 1){
|
if(player.options_.sources.length > 1){
|
||||||
// Wait for player ready event
|
// Wait for player ready event
|
||||||
|
@ -261,9 +266,9 @@
|
||||||
player.updateSrc(player.options_.sources);
|
player.updateSrc(player.options_.sources);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// register the plugin
|
// register the plugin
|
||||||
videojs.plugin('videoJsResolutionSwitcher', videoJsResolutionSwitcher);
|
videojs.plugin('videoJsResolutionSwitcher', videoJsResolutionSwitcher);
|
||||||
})(window, videojs);
|
})(window, videojs);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue