ashketchup
ashketchup

Reputation: 11

Expo-AV play audios after another

I'm using Expo-AV and I'm trying to find a simple way to play 3 audio files after another.

I can't find anything online, does anyone have experience with Expo-AV?

Much appreciated.

Upvotes: 1

Views: 1571

Answers (2)

Lasitha Yapa
Lasitha Yapa

Reputation: 4609

This is how I could achieve this.

const sounds = [];
for (const note of notes) {
  const sound = new Audio.Sound();
  await sound.loadAsync(soundFile);
  sounds.push(sound);
}

for (const sound of sounds) {
  await sound.setStatusAsync({ shouldPlay: true });

  await new Promise((resolve, reject) => {
    sound.setOnPlaybackStatusUpdate((status: AVPlaybackStatus) => {
      if (status.positionMillis > status.durationMillis * 0.8) {
        console.log('Done playing');

        resolve(true);
      }
    });
  });
}

Note that in my scenario I mark the playing as done when playback is 80% completed. You can adjust the value or use positionMillis == durationMillis as well.

There are some other useful values in the status. This is what worked the best in my scenario. But have a look at the other values in status object.

Upvotes: 0

Abe
Abe

Reputation: 5508

The docs for expo-av show an example that plays from a playlist of media. It's a bit of a wordy example since it covers a lot of cases, but it does what you want it to do.

This line shows how they initialize Audio.Sound.createAsync - the third argument is a callback that runs when the playback state changes. In that callback, you can check if the audio has finished playing, and play the next sound if so. The example does this here.

Upvotes: 2

Related Questions