I am currently working on the drum machine project in React. I have a minimum viable product working but I want to push things further.
I have a record button on my project which record the button pressed and stores the sounds in an array in the state of my app. I want to create a loop by playing the sounds in the array on repeat.
I tried something like this:
let audio = new Audio();
var playlist = this.state.sequence; // load the sequence of sounds
audio.src = playlist[0].src; // set the source of the first file in my array
audio.play();
// when the song ends, load the new sound
audio.addEventListener('ended', function(){
// increment playlist[i].src
}, true);
audio.loop = false;
However, I don’t know if it’s the right way to do so… Is there a way to achieve such thing ?
hey,
yes you can use setInterval() function for this kind of thing and have it play a sound every second or however long you thinks best, you can have it repeat by putting an if statement at the bottom with something like if(i == playlist.length) i = 0;
hope this helps.