]> 105106.c2e0p.group Git - sound.git/blobdiff - sound.js
Support Audio.preload.
[sound.git] / sound.js
index f823be31f00e5338f75967e0ec7c3564ac09c844..df5202e61c8911f84561b79acfe39149bc63b58e 100644 (file)
--- a/sound.js
+++ b/sound.js
@@ -1,11 +1,12 @@
-function Sound() {
+function Sound(src) {
 
-       if (Sound.audioContext === undefined)
-               Sound.audioContext = new webkitAudioContext();
+       if (Sound.audioContext === undefined) {
+               var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
+               Sound.audioContext = new AudioContext();
+       }
 
-       this._src = null;
        this._networkState = this.NETWORK.EMPTY;
-       this._preload = true;
+       this._preload = this.PRELOAD.AUTO;
        this._buffered = {};
        this._readyState = this.READY.NOTHING;
        this._seeking = false;
@@ -15,7 +16,7 @@ function Sound() {
        this._played = {};
        this._seekable = {};
        this._ended = false;
-       this._autoplay = false;
+       this._autoplay = true;
        this._loop = false;
        this._volume = 1;
        this._muted = false;
@@ -26,11 +27,12 @@ function Sound() {
        this.gainNode = null;
 
        this.ajax = null;
-       this.eventListeners = { }; 
+       this.eventListeners = { };
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
-       this.load();
+
+       this.setSrc(src);
 }
 
 Sound.prototype = {
@@ -58,6 +60,12 @@ Sound.prototype = {
                ENOUGH_DATA: 4,
        },
 
+       PRELOAD: {
+               NONE: 0,
+               METADATA: 1,
+               AUTO: 2,
+       },
+
        load: function() {
                if (this.ajax)
                        this.ajax.abort();
@@ -75,7 +83,7 @@ Sound.prototype = {
 
                this.setPlaybackRate(this.defaultPlaybackRate);
                this._error = null;
-               this._autoplay = true;
+               this.shouldBePlaying = this._autoplay;
                this.stopInternal();
 
                if (!this._src) {
@@ -129,8 +137,9 @@ Sound.prototype = {
                if (this._ended && this._playbackRate > 0)
                        this.setCurrentTime(0);
 
-               if (this._paused) {
+               if (this._paused || this._ended) {
                        this._paused = false;
+                       this._ended = false;
                        this.dispatchEventAsync(new CustomEvent('play'));
 
                        if (this._readyState < this.READY.FUTURE_DATA)
@@ -146,7 +155,7 @@ Sound.prototype = {
 
        playInternal: function() {
                this.gainNode = Sound.audioContext.createGainNode();
-               this.gainNode.gain.value = this._volume;
+               this.gainNode.gain.value = this._muted ? 0 : this._volume;
                this.gainNode.connect(Sound.audioContext.destination);
 
                this.node = Sound.audioContext.createBufferSource();
@@ -162,7 +171,7 @@ Sound.prototype = {
                this._autoplay = false;
 
                if (!this._paused) {
-                       this._paused = false;
+                       this._paused = true;
                        this.dispatchEventAsync(new CustomEvent('timeupdate'));
                        this.dispatchEventAsync(new CustomEvent('pause'));
                }
@@ -186,6 +195,13 @@ Sound.prototype = {
        },
 
        onended: function() {
+               if (this._loop) {
+                       this.stopInternal();
+                       this.setCurrentTime(0);
+                       this.playInternal();
+                       return;
+               }
+
                this._ended = true;
                this.nextStartTime = 0;
                this.stopInternal();
@@ -236,7 +252,8 @@ Sound.prototype = {
 
        setSrc: function(src) {
                this._src = src;
-               this.load();
+               if (this._autoplay || this._preload != this.PRELOAD.NONE)
+                       this.load();
        },
 
        getCurrentSrc: function() {
@@ -256,21 +273,30 @@ Sound.prototype = {
        },
 
        getPreload: function() {
-               if (!this._preload)
-                       return 'none';
-               return 'auto';
+               switch (this._preload) {
+                       case this.PRELOAD.NONE: return 'none';
+                       case this.PRELOAD.METADATA: return 'metadata';
+                       case this.PRELOAD.AUTO: return 'auto';
+                       default: return '';
+               }
        },
 
        setPreload: function(preload) {
                switch (preload) {
-               case 'none':
-                       this._preload = false;
-                       break;
-               default:
-                       this._preload = true;
-                       if (!this.buffer)
-                               load();
-                       break;
+                       default:
+                       case 'none':
+                               this._preload = this.PRELOAD.NONE;
+                               break;
+                       case 'metadata':
+                               this._preload = this.PRELOAD.METADATA;
+                               if (this._networkState === this.NETWORK.EMPTY)
+                                       this.load();
+                               break;
+                       case 'auto':
+                               this._preload = this.PRELOAD.auto;
+                               if (this._networkState === this.NETWORK.EMPTY)
+                                       this.load();
+                               break;
                }
        },
 
@@ -280,7 +306,7 @@ Sound.prototype = {
                return this.nextStartTime + Sound.audioContext.currentTIme - this.startTime;
        },
 
-       setCurrentTime: function(time) { 
+       setCurrentTime: function(time) {
                this.nextStartTime = time;
                this.dispatchEventAsync(new CustomEvent('timeupdate'));
                if (!this.node)
@@ -319,12 +345,50 @@ Sound.prototype = {
        },
 
        setVolume: function(volume) {
+               if (this._volume === volume)
+                       return;
+
                this._volume = volume;
-               if (!this.gainNode)
+               this.dispatchEventAsync(new CustomEvent('volumechange'));
+
+               if (this.gainNode)
+                       this.gainNode.gain.value = this._muted ? 0 : this._volume;
+       },
+
+       getMuted: function() {
+               return this._muted;
+       },
+
+       setMuted: function(muted) {
+               if (this._muted === muted)
                        return;
 
-               this.gainNode.gain.value = volume;
+               this._muted = muted;
                this.dispatchEventAsync(new CustomEvent('volumechange'));
+
+               if (this.gainNode)
+                       this.gainNode.gain.value = this._muted ? 0 : this._volume;
+       },
+
+       getAutoplay: function() {
+               return this._autoplay;
+       },
+
+       setAutoplay: function(autoplay) {
+               if (this._autoplay === autoplay)
+                       return;
+
+               this._autoplay = autoplay;
+               if (this._autoplay && this._networkState === this.NETWORK.EMPTY)
+                       this.load();
+       },
+
+       getLoop: function() {
+               return this._loop;
+       },
+
+       setLoop: function(loop) {
+               this._loop = loop;
        },
 };
 
@@ -423,8 +487,17 @@ Object.defineProperty(Sound.prototype, 'defaultMuted', {
        set: Sound.prototype.setDefaultMuted,
 });
 
+Object.defineProperty(Sound.prototype, 'preload', {
+       get: Sound.prototype.getPreload,
+       set: Sound.prototype.setPreload,
+});
+
 document.createElement = function(elementName) {
        if (elementName === "Audio" || elementName === "audio")
                return new Sound();
        return Document.prototype.createElement.call(this, elementName);
+};
+
+window.Audio = function(src) {
+       return new Sound(src);
 };
\ No newline at end of file