]> 105106.c2e0p.group Git - sound.git/commitdiff
Add loop and autoplay support.
authorJer Noble <[email protected]>
Thu, 20 Feb 2014 21:08:03 +0000 (13:08 -0800)
committerJer Noble <[email protected]>
Thu, 20 Feb 2014 21:08:03 +0000 (13:08 -0800)
sound.html
sound.js

index 82020e64951cdcd44f8099f5e293ac026bc12aab..6f0a1f6fe19d7dfb29582244fe4ea61bd7c89c9f 100644 (file)
@@ -27,7 +27,7 @@
        function onload() {
                sound = new Sound();
                sound.src = 'Coin.wav';
        function onload() {
                sound = new Sound();
                sound.src = 'Coin.wav';
-               sound.play();
+               sound.autoplay = true;
                sound.addEventListener('ended', eventLogger);
                sound.addEventListener('play', eventLogger);
                sound.addEventListener('pause', eventLogger);
                sound.addEventListener('ended', eventLogger);
                sound.addEventListener('play', eventLogger);
                sound.addEventListener('pause', eventLogger);
@@ -49,6 +49,7 @@
                <button onclick="sound.pause()">pause</button>
                <button onclick="sound.muted = !sound.muted">mute</button>
                <input type="range" min="0" max="1" step="0.01" onchange="sound.volume = event.target.value" />
                <button onclick="sound.pause()">pause</button>
                <button onclick="sound.muted = !sound.muted">mute</button>
                <input type="range" min="0" max="1" step="0.01" onchange="sound.volume = event.target.value" />
+               <button onclick="sound.loop = !sound.loop">loop</button>
        </div>
 
 </body>
        </div>
 
 </body>
index 843be80670bcda818c25042a1b75f0dad78e527e..efddc125262dca3339cddd21786db7d6761837e3 100644 (file)
--- a/sound.js
+++ b/sound.js
@@ -30,7 +30,6 @@ function Sound() {
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
        this.shouldBePlaying = 0;
        this.startTime = 0;
        this.nextStartTime = 0;
-       this.load();
 }
 
 Sound.prototype = {
 }
 
 Sound.prototype = {
@@ -187,6 +186,13 @@ Sound.prototype = {
        },
 
        onended: function() {
        },
 
        onended: function() {
+               if (this._loop) {
+                       this.stopInternal();
+                       this.setCurrentTime(0);
+                       this.playInternal();
+                       return;
+               }
+
                this._ended = true;
                this.nextStartTime = 0;
                this.stopInternal();
                this._ended = true;
                this.nextStartTime = 0;
                this.stopInternal();
@@ -237,7 +243,8 @@ Sound.prototype = {
 
        setSrc: function(src) {
                this._src = src;
 
        setSrc: function(src) {
                this._src = src;
-               this.load();
+               if (this._autoplay && this._src != null)
+                       this.load();
        },
 
        getCurrentSrc: function() {
        },
 
        getCurrentSrc: function() {
@@ -344,6 +351,27 @@ Sound.prototype = {
                if (this.gainNode)
                        this.gainNode.gain.value = this._muted ? 0 : this._volume;
        },
                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;
+       },
 };
 
 Object.defineProperty(Sound.prototype, 'src', {
 };
 
 Object.defineProperty(Sound.prototype, 'src', {