1

In React Native expo-av, it doesn't seem to be possible that you can initialise more than one sound file into a single sound object. Moreover, it is not possible to use another variable other than 'sound' to initialise another sound object so that you can initialise 3 or 4 sound files all at once. Hence, there will be a slight pause between the playing of the sound files as the sound objects are re-initialised every time a sound file is finished playing. So is there a way to re-write the following code so that you can eliminate the slight pauses between the playing of the sound files? At the moment, calling the setOnPlaybackStatusUpdate() function on the sound object two times doesn't seem to be very ideal, especially if you were to have like 10 sound files being played one after another, which would require calling setOnPlaybackStatusUpdate() 10 times. (FYI: Those sound files are numbers so that it's best for them to be played smoothly one after another without pauses.)

const [sound, setSound] = useState();

let data = [
    require(`../../assets/en_num_0.mp3`),
    require(`../../assets/en_num_1.mp3`),
    require(`../../assets/en_num_2.mp3`),
    require(`../../assets/en_num_3.mp3`),
    require(`../../assets/en_num_4.mp3`),
    require(`../../assets/en_num_5.mp3`),
    require(`../../assets/en_num_6.mp3`),
    require(`../../assets/en_num_7.mp3`),
    require(`../../assets/en_num_8.mp3`),
    require(`../../assets/en_num_9.mp3`)
];

async function playSound(num1, num2, num3) {

    const { sound } = await Audio.Sound.createAsync(data[num1], { shouldPlay: true });

    setSound(sound);
    await sound.playAsync();

    sound.setOnPlaybackStatusUpdate(async (status) => {
        if (status.didJustFinish === true) {
         
            const { sound } = await Audio.Sound.createAsync(data[num2], { shouldPlay: true });
            setSound(sound);
          
            await sound.playAsync();
          
            sound.setOnPlaybackStatusUpdate(async (status) => {

                if (status.didJustFinish === true) {
                    const { sound } = await Audio.Sound.createAsync(data[num3], { shouldPlay: true });
             
                    setSound(sound);
                    await sound.playAsync();

                }
            });
        }
    })

}

0 Answers0