zmxv / react-native-sound

React Native module for playing sound clips

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sequentially play an array of sounds

AndreaBorg217 opened this issue · comments

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); 
  }
}