this._playbackRate = 1;
this._played = {};
this._seekable = {};
- this._ended = false;
this._autoplay = true;
this._loop = false;
this._volume = 1;
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)
this.node.buffer = this.buffer;
this.node.playbackRate.value = this._playbackRate;
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);
+ if (this._playbackRate < 0)
+ this.node.start(0, 0, this.nextStartTime);
+ else
+ this.node.start(0, this.nextStartTime, this.buffer.duration - this.nextStartTime);
this.timeUpdateTimer = setInterval(this.sendTimeUpdate.bind(this), 250);
},
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] = [];
}
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);
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();
+ }
}
},