dynamic label

This commit is contained in:
Kasper Moskwiak 2015-08-20 17:57:31 +02:00
parent 60c666e15c
commit b120a90942
4 changed files with 59 additions and 17 deletions

View file

@ -12,7 +12,13 @@ Setup sources dynamically:
<script src="videojs-resolution-switcher.js"></script> <script src="videojs-resolution-switcher.js"></script>
<script> <script>
videojs('video', { videojs('video', {
controls: true controls: true,
plugins: {
videoJsResolutionSwitcher: {
default: 'high',
dynamicLabel: true
}
}
}, function(){ }, function(){
// Add dynamically sources via updateSrc method // Add dynamically sources via updateSrc method
@ -33,7 +39,7 @@ Setup sources dynamically:
console.info('Source changed to %s', player.src()) console.info('Source changed to %s', player.src())
}) })
}).videoJsResolutionSwitcher(); })
</script> </script>
``` ```
@ -77,7 +83,8 @@ videojs('video', {
}) })
``` ```
### Avalible options: ### Avalible options:
* default - `{Number}|'low'|'high'` - default resolution. If any `Number` is passed plugin will try to choose source based on `res` parameter. If `low` or `high` is passed, plugin will choose respectively worse or best resolution (if `res` parameter is specified). If `res` parameter is not specified plugin assumes that sources array is sorted from best to worse. * default - `{Number}|'low'|'high'` - default resolution. If any `Number` is passed plugin will try to choose source based on `res` parameter. If `low` or `high` is passed, plugin will choose respectively worse or best resolution (if `res` parameter is specified). If `res` parameter is not specified plugin assumes that sources array is sorted from best to worse.
* dynamicLabel - `{Boolean}` - if `true` current label will be displayed in control bar. By default gear icon is displayed.
## Example ## Example

View file

@ -38,11 +38,11 @@
</p> </p>
</div> </div>
<video id="video_1" class="video-js vjs-default-skin" width="1000" controls data-setup='{}'> <video id="video_1" class="video-js vjs-default-skin" width="1000" controls data-setup='{}' muted>
<source src="http://mirrorblender.top-ix.org/movies/sintel-1024-surround.mp4" type='video/mp4' label='SD' res='480' /> <source src="http://mirrorblender.top-ix.org/movies/sintel-1024-surround.mp4?480" type='video/mp4' label='SD' res='480' />
<source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm" type='video/webm' label='HD' res='1080'/> <source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm?1080" type='video/webm' label='HD' res='1080'/>
<source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm" type='video/webm' label='phone' res='144'/> <source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm?144" type='video/webm' label='phone' res='144'/>
<source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm" type='video/webm' label='4k' res='2160'/> <source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm?2160" type='video/webm' label='4k' res='2160'/>
</video> </video>
<script src="node_modules/video.js/dist/video.js"></script> <script src="node_modules/video.js/dist/video.js"></script>
@ -55,7 +55,8 @@
width: 1000, width: 1000,
plugins: { plugins: {
videoJsResolutionSwitcher: { videoJsResolutionSwitcher: {
default: 'high' // Default resolution [{Number}, 'low', 'high'] default: 'high', // Default resolution [{Number}, 'low', 'high'],
dynamicLabel: true // Display dynamic labels or gear symbol
} }
} }
}, function(){ }, function(){

View file

@ -1,10 +1,24 @@
.vjs-resolution-button { .vjs-resolution-button {
color: #ccc; color: #ccc;
font-family: VideoJS font-family: VideoJS;
} }
.vjs-resolution-button:before { .vjs-resolution-button .vjs-resolution-button-staticlabel:before {
content: '\f110' content: '\f110';
font-size: 1.8em;
line-height: 1.67;
}
.vjs-resolution-button .vjs-resolution-button-label {
font-size: 1.2em;
line-height: 2.50em;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
box-sizing: inherit;
} }
.vjs-resolution-button ul.vjs-menu-content { .vjs-resolution-button ul.vjs-menu-content {

View file

@ -14,7 +14,10 @@
*/ */
videoJsResolutionSwitcher = function(options) { videoJsResolutionSwitcher = function(options) {
var settings = videojs.mergeOptions(defaults, options), var settings = videojs.mergeOptions(defaults, options),
player = this; player = this,
label = document.createElement('span');
label.classList.add('vjs-resolution-button-label')
/* /*
* Resolution menu item * Resolution menu item
@ -31,11 +34,17 @@
this.on('touchstart', this.onClick) this.on('touchstart', this.onClick)
}, },
onClick: function(){ onClick: function(){
console.log(this.el().parentNode.children)
this.el().parentNode.children.forEach(function(el){
el.classList.remove('vjs-selected')
})
// Hide bigPlayButton // Hide bigPlayButton
player.bigPlayButton.hide() player.bigPlayButton.hide()
// Remember player state // Remember player state
var currentTime = player.currentTime() var currentTime = player.currentTime()
var isPaused = player.paused() var isPaused = player.paused()
// Change menu button label
label.innerHTML = this.options_.label;
// Change player source and wait for loadedmetadata event, then play video // Change player source and wait for loadedmetadata event, then play video
player.src({src: this.src, type: this.type}).one( 'loadedmetadata', function() { player.src({src: this.src, type: this.type}).one( 'loadedmetadata', function() {
player.currentTime(currentTime) player.currentTime(currentTime)
@ -54,7 +63,15 @@
constructor: function(player, options){ constructor: function(player, options){
this.sources = options.sources; this.sources = options.sources;
MenuButton.call(this, player, options); MenuButton.call(this, player, options);
this.controlText('Quality') this.controlText('Quality');
if(settings.dynamicLabel){
this.el().appendChild(label)
}else{
var staticLabel = document.createElement('span')
staticLabel.classList.add('vjs-resolution-button-staticlabel')
this.el().appendChild(staticLabel)
}
}, },
createItems: function(){ createItems: function(){
var sources = this.sources; var sources = this.sources;
@ -79,13 +96,15 @@
player.controlBar.resolutionSwitcher.dispose() player.controlBar.resolutionSwitcher.dispose()
delete player.controlBar.resolutionSwitcher delete player.controlBar.resolutionSwitcher
} }
//Sort sourcec //Sort sources
src = src.sort(compareResolutions) src = src.sort(compareResolutions)
groupedSrc = bucketSources(src) groupedSrc = bucketSources(src)
var menuButton = new ResolutionMenuButton(player, { sources: src }); var menuButton = new ResolutionMenuButton(player, { sources: src });
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)
player.src(chooseSrc(src)); var newSource = chooseSrc(src)
label.innerHTML = newSource.label
player.src(newSource);
} }
/** /**
@ -130,11 +149,12 @@
if(settings.default === 'low'){ return src[src.length - 1] } if(settings.default === 'low'){ return src[src.length - 1] }
if(settings.default === 'high'){ return src[0] } if(settings.default === 'high'){ return src[0] }
if(groupedSrc.res[settings.default]){ return groupedSrc.res[settings.default][0] } if(groupedSrc.res[settings.default]){ return groupedSrc.res[settings.default][0] }
return src return src[src.length - 1]
} }
// 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){
console.log(player.options_.sources)
player.updateSrc(player.options_.sources) player.updateSrc(player.options_.sources)
} }