Expo AV unable to play multiple sounds

Please provide the following:

  1. SDK Version: 39.0.2
  2. Platforms: Android and iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

I’m trying to get multiple sounds files to play on an Expo-AV, however when one sound plays, the other stops. I can’t get more than one sound to play at a time.

What I tried:
I tried a class method to create a different instance each time but it didn’t work. SO I reverted back to the Functional Component and called playSound() to play at the same time but it doesn’t work

This is my code:

import React, { useEffect, useState } from "react";
import { Text, View} from "react-native";
import {Audio} from 'expo-av'

const MusicModal = (props) => {
  const [sound, setSound] = React.useState(new Audio.Sound());
  const [isPlaying, setIsPlaying] = React.useState(false);
  
  useEffect(()=>{
      handleAudioConfig()
},[])

async function playSound(song) {
  console.log('Loading Sound');
  const { sound } = await Audio.Sound.createAsync(song);
  setSound(sound);
  sound.setIsLoopingAsync(true)
  console.log('Playing Sound');
  await sound.playAsync().then(()=>{
  setIsPlaying(true)
}); 
}

useEffect(() => {
  return sound
    ? () => {
        console.log('Unloading Sound');
        sound.unloadAsync(); 
      }
    : undefined;

}, [sound]);

const handleAudioConfig = async() =>{
  try {
    await Audio.setAudioModeAsync({
      allowsRecordingIOS: false,
      interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
      playsInSilentModeIOS: true,
      interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
      shouldDuckAndroid: true,
      staysActiveInBackground: true,
      playThroughEarpieceAndroid: false,
  
    })
    playSound(require('soundFilePath'))
    playSound(require('soundFilePath'))
  } catch (e) {
    console.log(e)
  }  
}

  return (
      <View>
          <Text>Play Sound</Text>
    </View>
  );
};

export default MusicModal;

Any help or advice would really help to be able to play sounds at the same time. Thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.