Audio interruption handling in AVFoundation and starting audio in the background

I am building a iOS app with Ionic and Capacitor. I use AVFoundation inside a Capacitor plugin which provides a public method to play an audio file accessible at a remote URL. The basics work, the audio file is successfully played. Now I want to correctly handle interruptions and background audio. By background audio I mean start playing an audio file while the app is in the background and not continue playing it in the background when it was started in foreground before. To summarize, the app should behave the following way:
  • If another app (e.g. Spotify) is playing audio and my app wants to play audio, the other app should be interrupted and my app's audio should start playing without mixing.

  • If my app's audio is finished playing, the other app's audio should resume.

  • My app must be able to start playing audio without mixing when it is in the background.

  • I want to show a play/pause button and the audio title on the lockscreen and in the notification center.

I added the background mode audio capability. I use AVPlayer, AVAudioSession, MPNowPlayingInfoCenter and MPRemoteCommandCenter, followed best practices, especially regarding activation and deactivation of an audio session and interruption handling. I tried several solutions, but never managed to achieve all of the above goals. What I found out so far is the following:
  • Goal 1. above is easy to achieve. Simply activate an audio session without mixing and start playing the audio.

  • After the audio of my app is finished, I deactivate the audio session and use the option notifyOthersOnDeactivation. Unfortunately, I can only achieve goal 2. above if I don't use remote commands nor notification info. This means, my app then has no audio control UI on the lockscreen nor in the notification center. Somehow the configurations of MPNowPlayingInfoCenter and MPRemoteCommandCenter seem to affect the interruption behavior. As far as I could see, Apple documentation doesn't say anything about this.

  • I only manage to start playing audio in the background when setting the option mixWithOthers. Apple documentation doesn't say anything about this, either.

Any help would be greatly appreciated.

Rony
Hi Rony,

Did you ever find a solution to this? I'm struggling with a similar challenge where I want to pause audio from any other app while my app is playing, even when running in the background.

Best regards,

Eirik
Hi Eirik

Unfortunately, I haven't found a solution yet. I am still in discussion with Apple support. Below you will find the answers I have got so far. I will update this information as soon as I know more.
  • If an app is in the background (the user pressed the home or sleep button), by design, it can only start audio with mixing.

  • If an app registered for remote commands (MPNowPlayingInfoCenter, MPRemoteCommandCenter) and its audio is not mixable, when it becomes the "Now playing" app, by design, it causes other apps to stop their audio, rather than just interrupting them.

Cheers
Rony




I got a final answer from Apple support.

If you want to start audio from the background without mixing, the best you can do is to use category .playback and category options .duckOthers and .interruptSpokenAudioAndMixWithOthers (check options here). This means not spoken other audio will be mixed and ducked and may make it difficult to hear your app's audio.
Audio interruption handling in AVFoundation and starting audio in the background
 
 
Q