0

I'm using react-native-sound library in order to play one track multiple times. Here is my code:


 const playAudio = () => {
   sound.setNumberOfLoops(2).play()
 }

playAudio()

I wonder why the track is played infinite times.

Xenia
  • 87
  • 6

1 Answers1

0

The playAudio() shouldn’t be used directly. Use this

useEffect( () => {
   playAudio()
}, [] )

const playAudio = () => {
   // Loop indefinitely until stop() is called
   sound.setNumberOfLoops(-1).play()
}
FnH
  • 587
  • 4
  • 5
  • Like this it doesn't play at all. I tried using a button and it creates infinite loop. – Xenia Aug 24 '21 at 04:24
  • I have modified my answer. Try that. – FnH Aug 24 '21 at 05:02
  • sound.setNumberOfLoops(2).play() works the same way as sound.setNumberOfLoops(-1).play(). I need to play the track just twice but it doesn't stop automatically. I don't want infinite loop. – Xenia Aug 24 '21 at 09:32