2

I am trying to as the title reads play buzz.js sound objects one after another. I have tried using the events - ended callback but this becomes rigorous with a large set of files. I thought i could create my list of sounds, iterate through them and call the bind one time but that didn't work as the iteration doesn't wait for the call back to finish. I am using node.js.

Here is what i have so far:

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

How can i wait until sound1 has finished before sound2 starts playing in a dynamic way ?

Warz
  • 7,386
  • 14
  • 68
  • 120

1 Answers1

3

Probably not the most elegant solution, buzz allows you to define groups that you could use instead of an array but:

Try adding all of the files that you want to play to an array, then loop through said array binding an ended event to each element that triggers the next song to play.

An extremely simple example:

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
nicangeli
  • 108
  • 6
  • Still doesn't wait, all sounds are playing at the same time – Warz May 27 '13 at 19:21
  • edited the answer above to use the forEach loop to trigger the next file to play – nicangeli May 27 '13 at 20:04
  • There is a fork and pull request that handles playing sound objects created in a group when you set the playback Mode i.e 'chain' verses 'blend'. The original author has not pulled and merged it yet so i am running off the fork until then. Thanks for your idea here though – Warz May 27 '13 at 20:14
  • Awesome. group.setPlaybackMode(chain), group.play() just in case anyone has a similar use case. https://github.com/4lbertoC/buzz – nicangeli May 27 '13 at 20:26
  • Although, i wonder why the author didn't set playback mode for all other `sound` methods like `togglePlay & pause `? – Warz May 27 '13 at 20:37
  • i don't know if you had a chance to try the fork yet. It fails on bindings and some of the other features of the original branch. – Warz May 29 '13 at 17:31
  • Also, if anyone is using your next element solution, you need to check if there is something at `index+1` otherwise, you get an out of bounds for your array. `if(sounds[index+1]){ sounds[index+1].play(); }` – Warz May 29 '13 at 17:39