X-Git-Url: http://id.pley.net/sound.git/blobdiff_plain/cdba2888fe934336681d265c296e61bff2ca5342..2d410f3ec8d9a7889cc47086d3e6c30a29fb6d56:/sound.js?ds=sidebyside diff --git a/sound.js b/sound.js index 355cfd4..a00d422 100644 --- 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._playbackRate > 0) + 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); @@ -469,7 +483,7 @@ Sound.prototype = { }, setCurrentTime: function(time) { - this.nextStartTime = time; + this.nextStartTime = parseFloat(time); this.dispatchEventAsync(new CustomEvent('timeupdate')); if (!this.node) return; @@ -495,12 +509,19 @@ Sound.prototype = { setPlaybackRate: function(rate) { var oldPlaybackRate = this._playbackRate; - this._playbackRate = rate; + this._playbackRate = parseFloat(rate); + this.dispatchEventAsync(new CustomEvent('ratechange')); - if (this.buffer) { - this.nextStartTime = oldPlaybackRate * (Sound.audioContext.currentTime - this.startTime); - this.stopInternal(); - this.playInternal(); + if (this.node) { + 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(); + } } }, @@ -509,7 +530,8 @@ Sound.prototype = { }, setDefaultPlaybackRate: function(rate) { - this._defaultPlaybackRate = rate; + this._defaultPlaybackRate = parseFloat(rate); + this.dispatchEventAsync(new CustomEvent('ratechange')); }, getVolume: function() { @@ -520,7 +542,7 @@ Sound.prototype = { if (this._volume === volume) return; - this._volume = volume; + this._volume = parseFloat(volume); this.dispatchEventAsync(new CustomEvent('volumechange')); if (this.gainNode)