zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.78k stars 747 forks source link

Sequentially play an array of sounds #800

Closed AndreaBorg217 closed 1 year ago

AndreaBorg217 commented 1 year ago

I am using the function below to attempt to play the array (a,b,c are all Sound instances initialised beforehand) of sounds (notes) sequentially (a is played and ends, then b is played and ends etc.). However instead of playing each sound one after the other it is playing all the sounds together in one go. How can this code be modified to play the sounds sequentially?

const playSound = () =>{
    const notes = [a,b,c,a]
    notes.forEach(element => {element.setVolume(1).play()});
}

I have also attempted to use the code below however it plays the first note only in an infinite loop;

let notes = [a,b,c,a]

 const playMelody= () =>{
    let index = 0;
    const final = notes.length

   while(index != final){
      console.log(index)
      let sound = notes[index]
      sound.play((success) => index = index+1); 
  }
}