+/* Copyright (c) 2014 Jer Noble
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
function Sound(src) {
if (Sound.audioContext === undefined) {
this.ajax = null;
this.eventListeners = { };
- this.shouldBePlaying = 0;
this.startTime = 0;
this.nextStartTime = 0;
+ this.autoplaying = false;
+ this.delayingTheLoadEvent = false;
+
this.setSrc(src);
}
this.setPlaybackRate(this.defaultPlaybackRate);
this._error = null;
- this.shouldBePlaying = this._autoplay;
+ this.autoplaying = true;
this.stopInternal();
+ this.selectResource();
+
+ },
+
+ selectResource: function() {
+ this._networkState = this.NETWORK.NO_SOURCE;
+ this.delayingTheLoadEvent = true;
+
+ setTimeout(this.selectResourceAsync.bind(this), 0);
+ },
+
+ selectResourceAsync: function() {
if (!this._src) {
this._networkState = this.NETWORK.EMPTY;
return;
this._networkState = this.NETWORK.LOADING;
this.dispatchEventAsync(new CustomEvent('loadstart'));
+ setTimeout(this.fetchResource(), 0);
+ },
+
+ fetchResource: function() {
+ if (this._preload === this.PRELOAD.NONE) {
+ this._networkState = this.NETWORK.IDLE;
+ this.dispatchEventAsync(new CustomEvent('suspend'));
+ this.delayingTheLoadEvent = false;
+ return;
+ }
+
this.ajax = new XMLHttpRequest();
this.ajax.open("GET", this._src, true);
this.ajax.responseType = "arraybuffer";
if (!this.ajax.response)
return;
+ this._networkState = this.NETWORK.IDLE;
+ this.dispatchEventAsync(new CustomEvent('suspend'));
this.setReadyState(this.READY.FUTURE_DATA);
try {
Sound.audioContext.decodeAudioData(
- this.ajax.response,
+ this.ajax.response,
function(buffer) {
this.buffer = buffer;
- if (this.shouldBePlaying)
+ if (this.autoplaying && this._paused && this._autoplay)
this.play();
- }.bind(this),
- function(error) {
- console.log("Error in creating buffer for sound '" + this._src + "': " + error);
+ this.dispatchEventAsync(new CustomEvent('canplaythrough'));
+ }.bind(this),
+ function(error) {
+ console.log("Error in creating buffer for sound '" + this._src + "': " + error);
}.bind(this)
);
} catch(exception) {
this.ajax.onprogress = function() {
this.dispatchEventAsync(new CustomEvent('progress'));
}.bind(this);
+ this.ajax.onerror = function() {
+ this.error = { code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED };
+ this._networkState = this.NETWORK.NO_SOURCE;
+ this.dispatchEventAsync(new CustomEvent('error'));
+ this.delayingTheLoadEvent = false;
+ }.bind(this);
this.ajax.send();
},
play: function() {
+ if (this._networkState === this.NETWORK.EMPTY)
+ this.loadResource();
+
if (!this.buffer) {
- this.shouldBePlaying = true;
+ this.autoplaying = true;
return;
}
pause: function() {
+ if (this._networkState === this.NETWORK.EMPTY)
+ this.loadResource();
+
this._autoplay = false;
if (!this._paused) {
setSrc: function(src) {
this._src = src;
- if (this._autoplay || this._preload != this.PRELOAD.NONE)
+ if (this._src)
this.load();
},