From c8edde1dfc7fdb9975cc0aacf02f3750c37db2bf Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 8 Dec 2015 12:30:28 +0200 Subject: [PATCH] Add sounds and stop sound parsing --- public_html/js/Parser.js | 2 +- public_html/js/Views.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/public_html/js/Parser.js b/public_html/js/Parser.js index 281d3ca..c8a7b92 100644 --- a/public_html/js/Parser.js +++ b/public_html/js/Parser.js @@ -335,7 +335,7 @@ Parser.prototype.stop = function() { } if (this.match(TokenType.SOUND) || this.match(TokenType.SOUNDLOOP)) { var fade = this.matchFade(); - // ViewActivity.getInstance().stopSound(fade); + ViewActivity.getInstance().stopSound(fade); } if (this.match(TokenType.AMBIENCE)) { var fade = this.matchFade(); diff --git a/public_html/js/Views.js b/public_html/js/Views.js index 51c5e2b..1e64343 100644 --- a/public_html/js/Views.js +++ b/public_html/js/Views.js @@ -11,6 +11,7 @@ function Views(parser) { this.musicPlayerAudio = new Audio(); this.soundPlayerAudio = new Audio(); this.musicQueue = new Array(); + this.soundQueue = new Array(); this.backgroundName = ""; this.backgroundType = ""; @@ -358,3 +359,38 @@ Views.prototype.stopMusic = function (fade) { } }; +Views.prototype.sound = function (name, loop, fade) { + try { + this.stopSound(this.soundPlayerAudio.fade); + this.soundPlayerAudio.src = PathResolver.sound(name); + this.soundPlayerAudio.fade = fade; + var views = this; + this.soundPlayerAudio.addEventListener('ended', function () { + if (views.soundQueue.length > 0) { + views.sound(views.soundQueue.pop(), false, views.NO_FADE); + } else if (loop) { + this.currentTime = 0; + this.play(); + } + }); + this.soundPlayerAudio.play(); + if (fade.fadeIn) { + $(this.soundPlayerAudio).prop("volume", 0.0); + $(this.soundPlayerAudio).animate({volume: 1.0}, fade.duration * 1000); + } + } catch (e) { + console.log("sound: " + name + " " + e); + } +}; + +Views.prototype.stopSound = function (fade) { + if (this.soundPlayerAudio.paused) return; + + if (fade.fadeOut) { + $(this.soundPlayerAudio).animate({volume: 0.0}, fade.duration * 1000, function() { + this.pause(); + }); + } else { + this.soundPlayerAudio.pause(); + } +}; \ No newline at end of file