Skip to content Skip to sidebar Skip to footer

Only Second Audio Plays On Array

I declared ar array variable and added my two audios inside but when I hit .play only second element into array plays. any solutions ?

is equivalent to:

ar[1]

So, only the last element is played, because in fact you are selecting binding to the src property of the audio object the last element of the array, which is "songs/example2.mp3" or, really, trench03.

If you want to play both, you need to first create an Audio object for each of them, set the src attribute and loop them on click, by executing the play method of each:

var trench01 = 'songs/example1.m4a';
var trench03 = 'songs/example2.mp3';
var ar = [trench01, trench03];
var tracks = ar.map((trench) => {
   var audio = newAudio();
   audio.src = trench;
   return audio;
});
$('.play').click(function(){
    tracks.forEach(track => track.play());
})

Post a Comment for "Only Second Audio Plays On Array"