javascript - 使用uzz.js 播放一个又一个声音

标签 javascript node.js callback buzz.js

我正在尝试按照标题读取一个又一个地播放 buzz.js 声音对象。我尝试过使用 events - returned 回调,但这对于大量文件来说变得很严格。我以为我可以创建我的声音列表,迭代它们并调用一次绑定(bind),但这不起作用,因为迭代不会等待回调完成。我正在使用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 允许您定义可以使用的组而不是数组,但是:

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

一个非常简单的例子:

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 - 使用uzz.js 播放一个又一个声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16778993/

相关文章:

javascript - 如何从 HAR 文件中获取总网页响应时间?

node.js - 从nodejs在mongodb中插入一大堆对象

C++/CLI 包装器 : callback gets fired after the objects containing the memberfunction is deleted

flutter - 此参数在Flutter文档中传递给此回调函数的位置是什么?

javascript - 如何在 CodeIgniter 中创建自动完成搜索字段?

php - 如何将 php 语句插入 jquery/javascript

node.js - VueJS - 用于构建的 vue.config.js 代理配置

javascript - NodeJS 中的嵌套回调和异常处理

Javascript addEventListener 回调逻辑

javascript - 过滤器不适用于 React 中具有 null 值的对象