How do we repeat sounds with Expo.Audio.Sound?

#1

I’ve been following this example to make sound effects upon taking a photo (or reading a bar code).

Here is an excerpt of what I’ve been experimenting…

 constructor(props) {
    super(props);
    this.state = {
      sound: new Expo.Audio.Sound(),
    }
  }
 //
 async componentWillMount() {
    await this.state.sound.loadAsync(require('./assets/blah.mp3'));
  }
 //
 _handleBarCodeRead = async(val) => {
    try {
      await this.state.sound.playAsync(); //This works, but just only once!
    }
    catch (error) {
     //
    }
  }

Once _handleBarCodeRead is called, blah.mp3 is played as expected.
However, when I have _handleBarCodeRead called again, this sound is not played.
(In order to repeat the sound, I have to go to another component and come back to this component)

How do we play a loaded sound resource multiple times without changing views?
Any advice will be appreciated.

1 Like
#2

hey hope this might help here is a link to a sampler that plays sounds as many times as you press a button. https://expo.io/@shinedark/M_SamplerTest here is the link to the repo https://github.com/shinedark/rnmsampler hope it helps. make sure where you test is not set to silent.

#3

I would recommend reseting the sound before playing it:

await sound.setPositionAsync(0);
await sound.playAsync();

Here is an example: https://snack.expo.io/@community/flappy-bird

1 Like
#4

Works!
Thank you so much!

1 Like