Skip to content

Commit

Permalink
Added support for multiple time ranges in the load progress bar
Browse files Browse the repository at this point in the history
closes #1253

Display a true load progress (with accounting all buffered regions)

Add played property for player

Correctly display played regions in progress-bar

Support #played in MediaFaker so tests would pass

Remove play-progress regions but add some opacity to play-progress

Remove opacity

Revert "Add played property for player"

This reverts commit 6d8bf98.

Fixed an issue where a load progress of NaN was throwing an error

Reverted some remaining `played` changes

Updated the progress bar to encapsulate time ranges

In the previous commit the progress bar was broken up into timeranges.
This had the effect of looking broken, so now the progress bar covers
all time ranges, and the time ranges a subtley different in color
from the progress bar.
  • Loading branch information
brainopia authored and heff committed Aug 1, 2014
1 parent 6921b55 commit b2fbc80
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
12 changes: 10 additions & 2 deletions src/css/video-js.less
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ fonts to show/hide properly.

/* Progress Bars */
.vjs-default-skin .vjs-progress-holder .vjs-play-progress,
.vjs-default-skin .vjs-progress-holder .vjs-load-progress {
.vjs-default-skin .vjs-progress-holder .vjs-load-progress,
.vjs-default-skin .vjs-progress-holder .vjs-load-progress div {
position: absolute;
display: block;
height: 100%;
Expand Down Expand Up @@ -424,7 +425,14 @@ fonts to show/hide properly.
}
.vjs-default-skin .vjs-load-progress {
background: rgb(100, 100, 100) /* IE8- Fallback */;
background: rgba(255, 255, 255, 0.4);
background: rgba(255, 255, 255, 0.2);
}

/* there are child elements of the load progress bar that represent the
specific time ranges that have been buffered */
.vjs-default-skin .vjs-load-progress div {
background: rgb(120, 120, 120) /* IE8- Fallback */;
background: rgba(255, 255, 255, 0.1);
}

.vjs-default-skin .vjs-seek-handle {
Expand Down
39 changes: 34 additions & 5 deletions src/js/control-bar/progress-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ vjs.SeekBar.prototype.stepBack = function(){
this.player_.currentTime(this.player_.currentTime() - 5); // more quickly rewind for keyboard-only users
};


/**
* Shows load progress
*
Expand All @@ -125,16 +124,46 @@ vjs.LoadProgressBar = vjs.Component.extend({

vjs.LoadProgressBar.prototype.createEl = function(){
return vjs.Component.prototype.createEl.call(this, 'div', {
className: 'vjs-load-progress',
innerHTML: '<span class="vjs-control-text">Loaded: 0%</span>'
className: 'vjs-load-progress'
});
};

vjs.LoadProgressBar.prototype.update = function(){
if (this.el_.style) { this.el_.style.width = vjs.round(this.player_.bufferedPercent() * 100, 2) + '%'; }
var i, start, end, part,
buffered = this.player_.buffered(),
duration = this.player_.duration(),
bufferedEnd = this.player_.bufferedEnd(),
children = this.el_.children,
// get the percent width of a time compared to the total end
percentify = function (time, end){
var percent = (time / end) || 0; // no NaN
return (percent * 100) + '%';
};

// update the width of the progress bar
this.el_.style.width = percentify(bufferedEnd, duration);

// add child elements to represent the individual buffered time ranges
for (i = 0; i < buffered.length; i++) {
start = buffered.start(i),
end = buffered.end(i),
part = children[i];

if (!part) {
part = this.el_.appendChild(vjs.createEl())
};

// set the percent based on the width of the progress bar (bufferedEnd)
part.style.left = percentify(start, bufferedEnd);
part.style.width = percentify(end - start, bufferedEnd);
};

// remove unused buffered range elements
for (i = children.length; i > buffered.length; i--) {
this.el_.removeChild(children[i-1]);
}
};


/**
* Shows play progress
*
Expand Down
72 changes: 54 additions & 18 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,17 @@ vjs.Player.prototype.trackProgress = function(){

this.progressInterval = setInterval(vjs.bind(this, function(){
// Don't trigger unless buffered amount is greater than last time
// log(this.cache_.bufferEnd, this.buffered().end(0), this.duration())
/* TODO: update for multiple buffered regions */
if (this.cache_.bufferEnd < this.buffered().end(0)) {

var bufferedPercent = this.bufferedPercent();

if (this.cache_.bufferedPercent != bufferedPercent) {
this.trigger('progress');
} else if (this.bufferedPercent() == 1) {
}

this.cache_.bufferedPercent = bufferedPercent;

if (bufferedPercent == 1) {
this.stopTrackingProgress();
this.trigger('progress'); // Last update
}
}), 500);
};
Expand Down Expand Up @@ -748,7 +752,6 @@ vjs.Player.prototype.remainingTime = function(){
// http://dev.w3.org/html5/spec/video.html#dom-media-buffered
// Buffered returns a timerange object.
// Kind of like an array of portions of the video that have been downloaded.
// So far no browsers return more than one range (portion)

/**
* Get a TimeRange object with the times of the video that have been downloaded
Expand All @@ -771,19 +774,13 @@ vjs.Player.prototype.remainingTime = function(){
* @return {Object} A mock TimeRange object (following HTML spec)
*/
vjs.Player.prototype.buffered = function(){
var buffered = this.techGet('buffered'),
start = 0,
buflast = buffered.length - 1,
// Default end to 0 and store in values
end = this.cache_.bufferEnd = this.cache_.bufferEnd || 0;

if (buffered && buflast >= 0 && buffered.end(buflast) !== end) {
end = buffered.end(buflast);
// Storing values allows them be overridden by setBufferedFromProgress
this.cache_.bufferEnd = end;
var buffered = this.techGet('buffered');

if (!buffered || !buffered.length) {
buffered = vjs.createTimeRange(0,0);
}

return vjs.createTimeRange(start, end);
return buffered;
};

/**
Expand All @@ -797,7 +794,46 @@ vjs.Player.prototype.buffered = function(){
* @return {Number} A decimal between 0 and 1 representing the percent
*/
vjs.Player.prototype.bufferedPercent = function(){
return (this.duration()) ? this.buffered().end(0) / this.duration() : 0;
var duration = this.duration(),
buffered = this.buffered(),
bufferedDuration = 0,
start, end;

if (!duration) {
return 0;
}

for (var i=0; i<buffered.length; i++){
start = buffered.start(i);
end = buffered.end(i);

// buffered end can be bigger than duration by a very small fraction
if (end > duration) {
end = duration;
}

bufferedDuration += end - start;
}

return bufferedDuration / duration;
};

/**
* Get the ending time of the last buffered time range
*
* This is used in the progress bar to encapsulate all time ranges.
* @return {Number} The end of the last buffered time range
*/
vjs.Player.prototype.bufferedEnd = function(){
var buffered = this.buffered(),
duration = this.duration(),
end = buffered.end(buffered.length-1);

if (end > duration) {
end = duration;
}

return end;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/js/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ vjs.Slider.prototype.update = function(){
}

// Set the new bar width
bar.el().style.width = vjs.round(barProgress * 100, 2) + '%';
if (bar) {
bar.el().style.width = vjs.round(barProgress * 100, 2) + '%';
}
};

vjs.Slider.prototype.calculateDistance = function(event){
Expand Down

0 comments on commit b2fbc80

Please sign in to comment.