在播放下一个声音之前等待一个声音停止

Waiting for a sound to stop before playing the next sound

您好,我在 Phaser 中构建了一个应用程序,想创建一个包含两个声音的序列:

  1. "You have"(加载并存储为 audioSound[0])
  2. "Finished"(同上,audioSound[1])

然后我可以继续重复使用 1 并在 2 中使用不同的 words/sentences。为此,我需要按顺序播放声音。

目前正在使用:

audioSound[0].play();

audioSound[1].play();

othercode...

两者同时播放。关于如何让声音按顺序播放并且只在第二个播放完后才继续 othercode... 有什么想法吗?

提前致谢!

编辑:在@Julian 的回答后 嗨 - 明确地说,我有以下代码将项目弹出到队列中然后播放它们。尽管队列中的项目按顺序播放,但队列播放事件之后的 function/code 与声音并行执行,而不是在声音结束时执行。

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();

    ...
}