From c69aa8a42b7f55276d35bf64a08e869654c3b0ce Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Mon, 28 May 2018 00:02:08 +0200 Subject: [PATCH 1/2] Avoid duration getter returning NaN before element has loaded --- src/js/plyr.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/plyr.js b/src/js/plyr.js index 4c984fd7..21c00fd3 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -494,11 +494,11 @@ class Plyr { // Faux duration set via config const fauxDuration = parseFloat(this.config.duration); - // True duration - const realDuration = this.media ? Number(this.media.duration) : 0; + // Media duration can be NaN before the media has loaded + const duration = (this.media || {}).duration || 0; - // If custom duration is funky, use regular duration - return !Number.isNaN(fauxDuration) ? fauxDuration : realDuration; + // If config duration is funky, use regular duration + return fauxDuration || duration; } /** From fac8a185ba4a945ba33a45cdfd0dd55c53edf532 Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Mon, 28 May 2018 00:33:17 +0200 Subject: [PATCH 2/2] Simplify currentTime setter and bail when media hasn't loaded --- src/js/plyr.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/js/plyr.js b/src/js/plyr.js index 21c00fd3..34b618bd 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -432,21 +432,16 @@ class Plyr { * @param {number} input - where to seek to in seconds. Defaults to 0 (the start) */ set currentTime(input) { - let targetTime = 0; - - if (utils.is.number(input)) { - targetTime = input; + // Bail if media duration isn't available yet + if (!this.duration) { + return; } - // Normalise targetTime - if (targetTime < 0) { - targetTime = 0; - } else if (targetTime > this.duration) { - targetTime = this.duration; - } + // Validate input + const inputIsValid = utils.is.number(input) && input > 0; // Set - this.media.currentTime = targetTime; + this.media.currentTime = inputIsValid ? Math.min(input, this.duration) : 0; // Logging this.debug.log(`Seeking to ${this.currentTime} seconds`);