From 1b057dfe858c07e44f7217b68322eb8352e0d0d7 Mon Sep 17 00:00:00 2001 From: heff Date: Thu, 3 Sep 2015 17:32:19 -0700 Subject: [PATCH] Cleaned up durationchange process in the player --- CHANGELOG.md | 1 + src/js/player.js | 57 +++++++++++++++++++----------------------------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e40098acc7..ce2ca01437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) -------------------- diff --git a/src/js/player.js b/src/js/player.js index 297f6856b6..fba12670e7 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -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) @@ -867,8 +870,7 @@ class Player extends Component { * @event durationchange */ handleTechDurationChange() { - this.updateDuration(); - this.trigger('durationchange'); + this.duration(this.techGet('duration')); } /** @@ -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 * @@ -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; } /**