From 8c62506f54426b857f06ca656124cc733376b33c Mon Sep 17 00:00:00 2001 From: Natalie Harris Date: Fri, 24 Jul 2015 10:51:30 -0700 Subject: [PATCH] Update fast-forward and rewind to work with play/pause. Closes #130. Change-Id: I33ed2e2ac0014eb3b271f72904996d669b0f13ee --- controls.js | 3 +++ lib/player/player.js | 30 ++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/controls.js b/controls.js index 5781335dd8..54e013c728 100644 --- a/controls.js +++ b/controls.js @@ -65,6 +65,7 @@ playerControls.init = function(video) { // play playButton.addEventListener('click', function() { if (!video.src) return; + playerControls.player_.setPlaybackRate(1); video.play(); }); video.addEventListener('play', function() { @@ -247,6 +248,7 @@ playerControls.onRewind = function() { var rate = playerControls.player_.getPlaybackRate(); playerControls.player_.setPlaybackRate( rate > 0 || rate < -4 ? -1.0 : rate * 2); + playerControls.video_.play(); }; @@ -258,6 +260,7 @@ playerControls.onFastForward = function() { if (!playerControls.player_) return; var rate = playerControls.player_.getPlaybackRate(); playerControls.player_.setPlaybackRate(rate < 0 || rate > 4 ? 1.0 : rate * 2); + playerControls.video_.play(); }; diff --git a/lib/player/player.js b/lib/player/player.js index 759cf0ff69..1a5b442f97 100644 --- a/lib/player/player.js +++ b/lib/player/player.js @@ -409,6 +409,7 @@ shaka.player.Player.prototype.onPause_ = function(event) { if (!isNaN(elapsed)) { this.stats_.logPlayTime(elapsed); } + this.cancelRewindTimer_(); }; @@ -872,12 +873,20 @@ shaka.player.Player.prototype.cancelWatchdogTimer_ = function() { shaka.player.Player.prototype.onRewindTimer_ = function(startVideoTime, startWallTime, rate) { shaka.asserts.assert(rate < 0); - var offset = ((Date.now() - startWallTime) / 1000) * rate; - this.video_.currentTime = startVideoTime + offset; - - var callback = this.onRewindTimer_.bind( - this, startVideoTime, startWallTime, rate); - this.rewindTimer_ = window.setTimeout(callback, 100); + var rewindOffset = + shaka.player.Player.REWIND_UPDATE_INTERVAL_ * Math.abs(rate); + if (this.video_.buffered.length && + this.video_.buffered.start(0) + rewindOffset < this.video_.currentTime) { + var offset = ((Date.now() - startWallTime) / 1000) * rate; + this.video_.currentTime = startVideoTime + offset; + + var callback = this.onRewindTimer_.bind( + this, startVideoTime, startWallTime, rate); + this.rewindTimer_ = window.setTimeout( + callback, shaka.player.Player.REWIND_UPDATE_INTERVAL_ * 1000); + } else { + this.video_.pause(); + } }; @@ -968,6 +977,15 @@ shaka.player.Player.UNDERFLOW_THRESHOLD_ = 0.1; shaka.player.Player.BUFFERED_FUDGE_FACTOR_ = 0.05; +/** + * The number of seconds for each rewind update interval. + * + * @private {number} + * @const + */ +shaka.player.Player.REWIND_UPDATE_INTERVAL_ = 0.1; + + /** * A map of MediaError codes to error messages. The JS interpreter won't take * a symbolic name as a key, so the symbolic names for these error codes appear