我正在尝试像标题一样逐个读取播放buzz.js声音对象。我尝试使用events - ended回调,但是对于大量文件而言,这变得很严格。我以为我可以创建声音列表,遍历它们,然后一次调用绑定,但这没有用,因为迭代不等待回调完成。我正在使用node.js。

这是我到目前为止所拥有的:

 var mySound1 = new buzz.sound("/sound/001000.mp3");
 var mySound2 = new buzz.sound('/sound/001001.mp3');
 var mySound3 = new buzz.sound('/sound/001002.mp3');


        for (var i = 0, max = 3; i < max; i++) {
            var sPath = '/sound/001' + soundArray[i].value + '.mp3';

            var sound1 = new buzz.sound(sPath);
             sound1.play().bind('ended', function(e){
                      //here i want sound1 to finish before sound2 plays and so forth
             });
        }


我如何才能等到sound1完成后再以动态方式播放sound2?

最佳答案

buzz可能不是最优雅的解决方案,它允许您定义可以使用的组而不是数组,但是:

尝试将所有要播放的文件添加到数组,然后遍历该数组,将结束事件绑定到触发下一首歌曲播放的每个元素。

一个非常简单的例子:

var files = [new buzz.sound('audio1.mp3'), new buzz.sound('audio2.mp3'), new buzz.sound('audio3.mp3')];

files.forEach(function(element, index) {
    element.bind('ended', function(e) { // when current file ends
        files[index+1].play(); // trigger the next file to play
    });
})

files[0].play(); // start the first file that triggers the rest

关于javascript - 用buzz.js播放一种声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16778993/

10-12 00:09
查看更多