Separate soundloop from sound

This commit is contained in:
Victor 2015-12-09 11:16:17 +02:00
parent 5337b544a9
commit dff9843c37
2 changed files with 51 additions and 15 deletions

View File

@ -298,9 +298,8 @@ Parser.prototype.hide = function() {
Parser.prototype.play = function() {
if (this.match(TokenType.MUSIC)) return this.playMusic();
if (this.match(TokenType.AMBIENCE)) return this.playAmbience();
if (this.lookMatch(0, TokenType.SOUND) || this.lookMatch(0, TokenType.SOUNDLOOP)) {
return this.playSound();
}
if (this.match(TokenType.SOUND)) return this.playSound();
if (this.match(TokenType.SOUNDLOOP)) return this.playSoundLoop();
return false;
};
@ -312,13 +311,16 @@ Parser.prototype.playMusic = function() {
};
Parser.prototype.playSound = function() {
var loop = false;
if (this.match(TokenType.SOUND)) loop = false;
else if (this.match(TokenType.SOUNDLOOP)) loop = true;
var name = this.consumeMusicName();
var fade = this.matchFade();
ViewActivity.getInstance().sound(name, loop, fade);
ViewActivity.getInstance().sound(name, fade);
return false;
};
Parser.prototype.playSoundLoop = function() {
var name = this.consumeMusicName();
var fade = this.matchFade();
ViewActivity.getInstance().soundLoop(name, fade);
return false;
};
@ -352,10 +354,14 @@ Parser.prototype.stop = function() {
var fade = this.matchFade();
ViewActivity.getInstance().stopMusic(fade);
}
if (this.match(TokenType.SOUND) || this.match(TokenType.SOUNDLOOP)) {
if (this.match(TokenType.SOUND)) {
var fade = this.matchFade();
ViewActivity.getInstance().stopSound(fade);
}
if (this.match(TokenType.SOUNDLOOP)) {
var fade = this.matchFade();
ViewActivity.getInstance().stopSoundLoop(fade);
}
if (this.match(TokenType.AMBIENCE)) {
var fade = this.matchFade();
ViewActivity.getInstance().stopAmbience(fade);

View File

@ -10,6 +10,7 @@ function Views(parser) {
this.musicPlayerAudio = new Audio();
this.soundPlayerAudio = new Audio();
this.soundLoopPlayerAudio = new Audio();
this.ambiencePlayerAudio = new Audio();
this.musicQueue = new Array();
this.soundQueue = new Array();
@ -370,7 +371,7 @@ Views.prototype.addSoundToQueue = function (name) {
if (!this.soundPlayerAudio.paused) {
this.soundQueue.push(name);
} else {
this.sound(name, false, this.NO_FADE);
this.sound(name, this.NO_FADE);
}
};
@ -410,7 +411,7 @@ Views.prototype.stopMusic = function (fade) {
}
};
Views.prototype.sound = function (name, loop, fade) {
Views.prototype.sound = function (name, fade) {
try {
this.stopSound(this.soundPlayerAudio.fade);
this.soundPlayerAudio.src = PathResolver.sound(name);
@ -418,10 +419,7 @@ Views.prototype.sound = function (name, loop, 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();
views.sound(views.soundQueue.pop(), views.NO_FADE);
}
});
this.soundPlayerAudio.play();
@ -446,6 +444,38 @@ Views.prototype.stopSound = function (fade) {
}
};
Views.prototype.soundLoop = function (name, fade) {
try {
this.stopSoundLoop(this.soundLoopPlayerAudio.fade);
this.soundLoopPlayerAudio.src = PathResolver.sound(name);
this.soundLoopPlayerAudio.fade = fade;
this.soundLoopPlayerAudio.addEventListener('ended', function () {
this.currentTime = 0;
this.play();
});
this.soundLoopPlayerAudio.play();
if (fade.fadeIn) {
$(this.soundLoopPlayerAudio).prop("volume", 0.0);
$(this.soundLoopPlayerAudio).animate({volume: 1.0}, fade.duration * 1000);
}
} catch (e) {
console.log("soundloop: " + name + " " + e);
}
};
Views.prototype.stopSoundLoop = function (fade) {
if (this.soundLoopPlayerAudio.paused) return;
if (fade.fadeOut) {
$(this.soundLoopPlayerAudio).animate({volume: 0.0}, fade.duration * 1000, function() {
this.pause();
});
} else {
this.soundLoopPlayerAudio.pause();
}
};
Views.prototype.ambience = function (name, fade) {
try {
this.stopAmbience(this.ambiencePlayerAudio.fade);