]> 105106.c2e0p.group Git - sound.git/blobdiff - sound.js
Fix .muted when playing.
[sound.git] / sound.js
index f823be31f00e5338f75967e0ec7c3564ac09c844..c4f9c0eb27329c84bbfa0564a678ea1e13f8945f 100644 (file)
--- a/sound.js
+++ b/sound.js
@@ -30,7 +30,6 @@ function Sound() {
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
-       this.load();
 }
 
 Sound.prototype = {
@@ -129,8 +128,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 +146,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 +162,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 +186,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 +243,8 @@ Sound.prototype = {
 
        setSrc: function(src) {
                this._src = src;
-               this.load();
+               if (this._autoplay && this._src != null)
+                       this.load();
        },
 
        getCurrentSrc: function() {
@@ -319,12 +327,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._src != null)
+                       this.load();
+       },
+
+       getLoop: function() {
+               return this._loop;
+       },
+
+       setLoop: function(loop) {
+               this._loop = loop;
        },
 };