kerosene kerosene -4 years ago 167
Javascript Question

JavaScript: Play multiple audio in a row

I'm trying to play multiple audio files in a row without a visual audio-player (HTML).

Here is my code:

var audio = new Audio('http://www.vogelstimmen-wehr.de/mp3/weidenmeise.mp3');
var index=0;
function playNext() {
if(index < 12) {
// change src depending on index (local files like 1.mp3, 2.mp3 so on)
audio.src = 'http://www.vogelstimmen-wehr.de/mp3/weidenmeise.mp3';
console.log("nothing?");
audio.load(); audio.play();
index += 1;
} else {
audio.removeEventListener('ended', playNext, false);
}


audio.addEventListener('ended', playNext);

audio.play();


Any help is appreciated!

Answer Source

It looks like you're missing a closing bracket for the playNext() function. This worked for me:

var audio = new Audio('http://www.vogelstimmen-wehr.de/mp3/weidenmeise.mp3');
var index=0; 
function playNext() {
    if(index < 12) {
        // change src depending on index (local files like 1.mp3, 2.mp3 so on)
        audio.src = 'http://www.vogelstimmen-wehr.de/mp3/weidenmeise.mp3';
        console.log("something!");
        audio.load(); audio.play();
        index += 1;
    } else {
        audio.removeEventListener('ended', playNext, false);
    }
}

audio.addEventListener('ended', playNext);

audio.play();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download