X-Git-Url: http://105106.c2e0p.group/sound.git/blobdiff_plain/ecabc9fd03e1fbecca682ab281cf19f996c381ee..5f1509d41e4c01615c4d50978fade3cd121925f5:/sound.js diff --git a/sound.js b/sound.js index e195ca6..8fcac31 100644 --- a/sound.js +++ b/sound.js @@ -185,7 +185,7 @@ Sound.prototype = { this.setNetworkState(this.NETWORK.IDLE); this.dispatchEventAsync(new CustomEvent('suspend')); - this.setReadyState(this.READY.FUTURE_DATA); + this.setReadyState(this.READY.METADATA); try { Sound.audioContext.decodeAudioData( @@ -210,7 +210,7 @@ Sound.prototype = { this.setCurrentTime(0); this.dispatchEventAsync(new CustomEvent('durationchange')); - this.setReadyState(this.READY.METADATA); + this.setReadyState(this.READY.ENOUGH_DATA); if (this.autoplaying && this._paused && this._autoplay) this.play(); @@ -239,8 +239,12 @@ Sound.prototype = { if (this.node) return; - if (this._ended && this._playbackRate > 0) - this.setCurrentTime(0); + if (this._ended) { + if (this._playbackRate > 0) + this.setCurrentTime(0); + else + this.setCurrentTime(this.duration) + } if (this._paused || this._ended) { this._paused = false; @@ -269,8 +273,9 @@ Sound.prototype = { this.node.connect(this.gainNode); this.node.buffer = this.buffer; this.node.playbackRate.value = this._playbackRate; - this.node.start(0, this.nextStartTime); this.node.onended = this.onended.bind(this); + var remainingDuration = this._playbackRate < 0 ? this.nextStartTime : this.buffer.duration - this.nextStartTime; + this.node.start(0, this.nextStartTime, remainingDuration); this.timeUpdateTimer = setInterval(this.sendTimeUpdate.bind(this), 250); }, @@ -294,7 +299,7 @@ Sound.prototype = { if (!this.buffer || !this.node) return; - this.nextStartTime = Sound.audioContext.currentTime - this.startTime; + this.nextStartTime = this._playbackRate * (Sound.audioContext.currentTime - this.startTime); this.stopInternal(); }, @@ -314,13 +319,11 @@ Sound.prototype = { onended: function() { if (this._loop) { this.stopInternal(); - this.setCurrentTime(0); this.playInternal(); return; } this._ended = true; - this.nextStartTime = 0; this.stopInternal(); this.dispatchEventAsync(new CustomEvent('ended')); }, @@ -408,7 +411,7 @@ Sound.prototype = { if (this.autoplaying && this._paused && this._autoplay && !this._ended && !this._error) { this.dispatchEventAsync('timeupdate'); this.dispatchEventAsync('waiting'); - this.nextStartTime = Sound.audioContext.currentTime - this.startTime; + this.nextStartTime = this._playbackRate * (Sound.audioContext.currentTime - this.startTime); this.stopInternal(); } } @@ -462,11 +465,11 @@ Sound.prototype = { getCurrentTime: function() { if (!this.node) return this.nextStartTime; - return this.nextStartTime + Sound.audioContext.currentTime - this.startTime; + return this.nextStartTime + this._playbackRate * (Sound.audioContext.currentTime - this.startTime); }, setCurrentTime: function(time) { - this.nextStartTime = time; + this.nextStartTime = parseFloat(time); this.dispatchEventAsync(new CustomEvent('timeupdate')); if (!this.node) return; @@ -491,14 +494,25 @@ Sound.prototype = { }, setPlaybackRate: function(rate) { - this._playbackRate = rate; + var oldPlaybackRate = this._playbackRate; + this._playbackRate = parseFloat(rate); + this.dispatchEventAsync(new CustomEvent('ratechange')); - if (this.buffer) { - this.stopInternal(); - this.playInternal(); + if (this.node) { + this.nextStartTime = oldPlaybackRate * (Sound.audioContext.currentTime - this.startTime); + this.node.playbackRate.value = this._playbackRate; } }, + getDefaultPlaybackRate: function() { + return this._defaultPlaybackRate; + }, + + setDefaultPlaybackRate: function(rate) { + this._defaultPlaybackRate = parseFloat(rate); + this.dispatchEventAsync(new CustomEvent('ratechange')); + }, + getVolume: function() { return this._volume; }, @@ -507,7 +521,7 @@ Sound.prototype = { if (this._volume === volume) return; - this._volume = volume; + this._volume = parseFloat(volume); this.dispatchEventAsync(new CustomEvent('volumechange')); if (this.gainNode)