Search code examples
androidiosreact-nativeexpoexpo-av

Expo-AV Audio crashing on Android on play


So I'm fairly new to using EXPO and building apps. I've been tasked to add a media player to a client's app and the setup I have currently works fine on iOS on both physical and emulator devices but when the play action is issued, the app crashes on Android, both physical and emulator devices.

Any help would be greatly appreciated. Below is a snippet of my code.

async function playSound() {
    console.log(`Loading link`);
    setIPlayer(true);
    
    const { sound } = await Audio.Sound.createAsync(
        source = {
            uri : "https://radio.canstream.co.uk:9129/live.mp3"
        }
    );

    setSound(sound);
    setIsPlaying(true);

    await sound.playAsync();
    
}

async function stopSound () {
    // await sound.stopAsync();
    await sound.pauseAsync();
    setIPlayer(false);
    setIsPlaying(false); 
}

useEffect(() => {

    const getNetwork = async () => {
        const res = await Network.getNetworkStateAsync();
        if(res.isConnected === false && res.isInternetReachable === false){
            setError(true);
        }
    }

    getNetwork();

    Audio.setAudioModeAsync({
        allowsRecordingIOS: false,
        // interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
        interruptionModeIOS: InterruptionModeIOS.DoNotMix,
        playsInSilentModeIOS: true,
        // interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
        interruptionModeAndroid: InterruptionModeAndroid.DuckOthers,
        shouldDuckAndroid: true,
        staysActiveInBackground: true,
        playThroughEarpieceAndroid: false,
        androidImplementation: 'MediaPlayer'
    })

    return sound ? () => {
        sound.unloadAsync(); }
    : undefined;
}, [sound]);

Solution

  • So the crashing issue was caused by a component that was being toggled when the play action was triggered. Essentially a UI bug, had to tear down the app and rebuild from scratch just to spot it.

    Thanks everyone!