]> id.pley.net Git - sound.git/commitdiff
Make ended behave correctly, support -2x <> 2x rates, and fix possible muting behavio... origin/gh-pages
authorJer Noble <jer.noble@apple.com>
Tue, 27 Jan 2015 21:15:10 +0000 (22:15 +0100)
committerJer Noble <jer.noble@apple.com>
Tue, 27 Jan 2015 21:15:10 +0000 (22:15 +0100)
sound.html
sound.js

index af49cce23d2ec94c98e80b68649fa287f81f4cfb..af92487fed4a92d50c0306d2005b32ab7bc0adef 100644 (file)
@@ -97,7 +97,7 @@
                <span id="duration">--:--</span>
                <button onclick="audio.loop = !audio.loop">loop</button>
                <button onclick="audio.playbackRate = -audio.playbackRate">reverse</button>
-               <input id="rate" type="range" min="-1" max="1" step="0.01" value="1" oninput="audio.playbackRate = event.target.value" />
+               <input id="rate" type="range" min="-2" max="2" step="0.01" value="1" oninput="audio.playbackRate = event.target.value" />
        </div>
 
 </body>
index 8fcac31691dd2ad8bc57ef8eced6900e9cd5eaba..a00d4223d34477b612d62aacd482fd209485bdac 100644 (file)
--- a/sound.js
+++ b/sound.js
@@ -36,7 +36,6 @@ function Sound(src) {
     this._playbackRate = 1;
     this._played = {};
     this._seekable = {};
-    this._ended = false;
     this._autoplay = true;
     this._loop = false;
     this._volume = 1;
@@ -239,16 +238,15 @@ Sound.prototype = {
         if (this.node)
             return;
 
-        if (this._ended) {
+        if (this.endedPlayback()) {
             if (this._playbackRate > 0)
                 this.setCurrentTime(0);
             else
                 this.setCurrentTime(this.duration)
         }
 
-        if (this._paused || this._ended) {
+        if (this._paused || this.endedPlayback()) {
             this._paused = false;
-            this._ended = false;
             this.dispatchEventAsync(new CustomEvent('play'));
 
             if (this._readyState < this.READY.FUTURE_DATA)
@@ -318,16 +316,32 @@ Sound.prototype = {
 
     onended: function() {
         if (this._loop) {
+            this.nextStartTime = this._playbackRate < 0 ? this.duration : 0;
             this.stopInternal();
             this.playInternal();
             return;
         }
 
-        this._ended = true;
+        this.nextStartTime = this._playbackRate < 0 ? 0 : this.duration;
         this.stopInternal();
         this.dispatchEventAsync(new CustomEvent('ended'));
     },
 
+    endedPlayback: function() {
+        if (this._readyState < this.READY.METADATA)
+            return false;
+
+        if (this.currentTime >= this.duration && this._playbackRate >= 0 && !this._loop)
+            return true;
+
+        if (this.currentTime <= 0 && this._playbackRate <= 0)
+            return true;
+    },
+
+    getEnded: function() {
+        return this.endedPlayback() && this._playbackRate >= 0;
+    },
+
     addEventListener: function(eventName, handler) {
         if (!this.eventListeners[eventName])
             this.eventListeners[eventName] = [];
@@ -408,7 +422,7 @@ Sound.prototype = {
         }
 
         if (oldState >= this.READY.FUTURE_DATA && newState <= this.READY.CURRENT_DATA) {
-            if (this.autoplaying && this._paused && this._autoplay && !this._ended && !this._error) {
+            if (this.autoplaying && this._paused && this._autoplay && !this.endedPlayback() && !this._error) {
                 this.dispatchEventAsync('timeupdate');
                 this.dispatchEventAsync('waiting');
                 this.nextStartTime = this._playbackRate * (Sound.audioContext.currentTime - this.startTime);
@@ -499,8 +513,15 @@ Sound.prototype = {
         this.dispatchEventAsync(new CustomEvent('ratechange'));
 
         if (this.node) {
-            this.nextStartTime = oldPlaybackRate * (Sound.audioContext.currentTime - this.startTime);
+            var currentTime = Sound.audioContext.currentTime
+            this.nextStartTime += oldPlaybackRate * (currentTime - this.startTime);
+            this.startTime = currentTime;
             this.node.playbackRate.value = this._playbackRate;
+
+            if ((oldPlaybackRate <= 0) != (this._playbackRate <= 0)) {
+                this.stopInternal();
+                this.playInternal();
+            }
         }
     },