Skip to content

Commit

Permalink
Cleaned up durationchange process in the player
Browse files Browse the repository at this point in the history
  • Loading branch information
heff committed Sep 7, 2015
1 parent 658adb2 commit 1b057df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ CHANGELOG
* @heff added back the default cdn url for the swf ([view](https://github.com/videojs/video.js/pull/2533))
* @gkatsev fixed the default state of userActive ([view](https://github.com/videojs/video.js/pull/2557))
* @heff fixed event bubbling in IE8 ([view](https://github.com/videojs/video.js/pull/2563))
* @heff cleaned up internal duration handling ([view](https://github.com/videojs/video.js/pull/2552))

--------------------

Expand Down
57 changes: 23 additions & 34 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ class Player extends Component {
this.techCall('setVolume', this.cache_.volume);
}

// Update the duration if available
this.handleTechDurationChange();

// Chrome and Safari both have issues with autoplay.
// In Safari (5.1.1), when we move the video element into the container div, autoplay doesn't work.
// In Chrome (15), if you have autoplay + a poster + no controls, the video gets hidden (but audio plays)
Expand Down Expand Up @@ -867,8 +870,7 @@ class Player extends Component {
* @event durationchange
*/
handleTechDurationChange() {
this.updateDuration();
this.trigger('durationchange');
this.duration(this.techGet('duration'));
}

/**
Expand Down Expand Up @@ -933,31 +935,6 @@ class Player extends Component {
event.preventDefault();
}

/**
* Update the duration of the player using the tech
*
* @private
* @method updateDuration
*/
updateDuration() {
// Allows for caching value instead of asking player each time.
// We need to get the techGet response and check for a value so we don't
// accidentally cause the stack to blow up.
var duration = this.techGet('duration');
if (duration) {
if (duration < 0) {
duration = Infinity;
}
this.duration(duration);
// Determine if the stream is live and propagate styles down to UI.
if (duration === Infinity) {
this.addClass('vjs-live');
} else {
this.removeClass('vjs-live');
}
}
}

/**
* Fired when the player switches in or out of fullscreen mode
*
Expand Down Expand Up @@ -1277,19 +1254,31 @@ class Player extends Component {
* @method duration
*/
duration(seconds) {
if (seconds !== undefined) {
if (seconds === undefined) {
return this.cache_.duration || 0;
}

// cache the last set value for optimized scrubbing (esp. Flash)
this.cache_.duration = parseFloat(seconds);
seconds = parseFloat(seconds) || 0;

return this;
// Standardize on Inifity for signaling video is live
if (seconds < 0) {
seconds = Infinity;
}

if (this.cache_.duration === undefined) {
this.updateDuration();
if (seconds !== this.cache_.duration) {
// Cache the last set value for optimized scrubbing (esp. Flash)
this.cache_.duration = seconds;

if (seconds === Infinity) {
this.addClass('vjs-live');
} else {
this.removeClass('vjs-live');
}

this.trigger('durationchange');
}

return this.cache_.duration || 0;
return this;
}

/**
Expand Down

0 comments on commit 1b057df

Please sign in to comment.