From 45b342640b26802ce9102a9e794a091f956c54a2 Mon Sep 17 00:00:00 2001 From: Jer Noble Date: Thu, 20 Feb 2014 13:08:03 -0800 Subject: [PATCH] Add loop and autoplay support. --- sound.html | 3 ++- sound.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/sound.html b/sound.html index 82020e6..6f0a1f6 100644 --- a/sound.html +++ b/sound.html @@ -27,7 +27,7 @@ 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); @@ -49,6 +49,7 @@ + diff --git a/sound.js b/sound.js index 843be80..efddc12 100644 --- 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 = { @@ -187,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(); @@ -237,7 +243,8 @@ Sound.prototype = { setSrc: function(src) { this._src = src; - this.load(); + if (this._autoplay && this._src != null) + this.load(); }, getCurrentSrc: function() { @@ -344,6 +351,27 @@ Sound.prototype = { 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', { -- 2.40.1