嗨,我已经在Phaser中构建了一个应用,并且想要创建两种声音的序列:
然后,我可以继续重复使用1,并在2中使用不同的单词/句子。为此,我需要按顺序播放声音。
目前使用:
audioSound[0].play();
audioSound[1].play();
othercode...
同时播放两者。关于如何使声音顺序播放以及仅在第二个声音结束后继续使用
othercode...
的任何想法?提前致谢!
编辑:@Julian回答之后
嗨-要清楚,我有以下代码将项目弹出到队列中,然后播放。尽管队列中的项目按顺序播放,但是队列播放事件之后的功能/代码与声音并行执行,而不是在声音结束时执行。
function playSequence(soundArray) {
soundArray[0].play();
soundArray.forEach(function(element, index, array) {
if (soundArray[index + 1]) {
soundArray[index].onStop.addOnce(function() {
soundArray[index + 1].play();
}, this);
}
});
最佳答案
您可以尝试以下方法:
function preload() {
game.load.audio('sound-explosion', 'explosion.wav');
game.load.audio('sound-lose', 'lose.mp3');
}
var soundlose;
var soundexplosion;
function create() {
//Add sounds
soundexplosion = game.add.audio('sound-explosion');
soundlose = game.add.audio('sound-lose');
//Prepare the next event once the sound finishes playing
soundexplosion.onStop.addOnce(function() { soundlose.play(); }, this);
soundlose.onStop.addOnce(function() { console.log('Hello!'); }, this)
//Play sound...
soundexplosion.play();
...
}
关于javascript - 等待声音停止播放下一个声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43424355/