How to prevent Audio from ducking other apps?

SDK Version: 36
Platforms: Android

Hello, I am trying to figure out how to prevent background audio from another app from being ducked (volume is lowered) when I use expo-av to play sounds.

Observed Behavior:

  1. Play music on Android device, from Spotify or Google Music, for example.
  2. Launch my expo app.
  3. Play a sound on my app. The sound on my app plays at full volume, but the music volume is lowered.

Desired Behavior:

  1. Play music on Android device, from Spotify or Google Music, for example.
  2. Launch my expo app.
  3. Play a sound on my app. The music from Spotify or Google plays at the same volume and my sounds mix like they do on iOS.

I tried the following settings but it did not work:

playSound = async(name, soundType) => {
    if (!isAudioEnabled()) return undefined;
    if (name === Sounds.none) return undefined;
    await Audio.setAudioModeAsync({
      allowsRecordingIOS: false,
      playsInSilentModeIOS: true,
      interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
      staysActiveInBackground: false,
      interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
      shouldDuckAndroid: true,
      playThroughEarpieceAndroid: true
    });
    const soundObject = new Audio.Sound();
    const source = this.getSource(name, soundType);
    try {
      await soundObject.loadAsync(source);
      await soundObject.playAsync();
    } catch (error) {
      Sentry.captureException(error);
    }
  };

Seems like settings interruptionModeAndroid and shouldDuckAndroid don’t really work as I expect them to.

My questions are as follows:

  • Is there a way to prevent the background music from having its volume lowered when I play sounds in my app? This would be my preferred solution.
  • Is there a way to detect if music is playing in the background before trying to play sounds from my app?
  • For iOS, there is the option INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS, but a similar option does not exist for Android. Is this perhaps a limitation of the Android OS?

Thanks in advance. This issue is driving me crazy.

1 Like

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