Blnukem
Blnukem

Reputation: 163

I want to play a multiple sound files base on queue

I want to play multiple sound files on my webpage based on queue order, example I have queue order like 1.wav 2.wav 3.wav, then I have my database to store the voice for each word 0-9.

I'm trying to create simple script to play sound in order on a single button click.

PLAY->1.wav -> END:PLAY-> 2.wav -> END:PLAY-> 3.wav -> END

Upvotes: 1

Views: 3563

Answers (1)

Loupax
Loupax

Reputation: 4914

I herd you liek recursion!

//This plays a file, and call a callback once it completed (if a callback is set)

function play(audio, callback) {

audio.play();
if(callback)
{
    audio.onended = callback;
}
}

This gets an array of audio objects and actually impements the queue

function queue_sounds(sounds){

    var index = 0;
    function recursive_play()
    {
      if(index+1 === sounds.length)
      {
        play(sounds[index],null);
      }
      else
      {
        play(sounds[index],function(){index++; recursive_play();});
      }
    }

recursive_play();   
}

So you actually do something like this:

queue_sounds([new Audio(foo), new Audio(bar), new Audio(lol)]);

Upvotes: 3

Related Questions