How Background Audio Works on iOS 15
iOS 15 allows audio to play continuously across different
applications through a combination of the system's Core Audio
architecture, background execution modes, and
AVAudioSession configurations. By decoupling the audio
rendering pipeline from the active app UI, the operating system can
suspend or limit an application's visual processes while keeping its
audio pipeline active, ensuring an uninterrupted listening experience
even when switching apps or locking the device.
Background Modes and Permissions
For an app to play audio while not in the foreground, developers must
explicitly declare the "Audio, AirPlay, and Picture in Picture"
background mode in the app’s Info.plist file. This tells
the iOS kernel that the process requires continuous background execution
rights specifically for media playback. Without this declaration, iOS
automatically pauses or terminates app processes within seconds of the
user returning to the Home Screen or switching to another task.
The Role of AVAudioSession
The primary software interface managing audio behavior in iOS 15 is
AVAudioSession. An app must configure its shared audio
session with the .playback category:
- Category Declaration: Setting the category to
AVAudioSession.Category.playbackinforms iOS that audio output is central to the app's function. - Mute Switch Bypass: This category ensures audio continues playing even if the physical Ring/Silent switch is set to silent.
- Screen Lock Persistence: It keeps the audio engine running even when the device display turns off.
System-Level Process Separation via mediaserverd
When an app plays audio, it does not directly manage the device
hardware from its own application sandbox. Instead, iOS routes the audio
stream through an operating-system-level daemon known as
mediaserverd.
When the user moves an app to the background:
- The app's user interface thread is suspended to conserve memory and battery.
- The audio buffers are continually fed into
mediaserverd. mediaserverdmaintains direct communication with the audio hardware, allowing the sound to persist even if the parent application is largely frozen in RAM.
Audio Session Mixing and Interruption Handling
iOS 15 uses priority rules to handle audio conflicts when switching
between different apps. Through AVAudioSession options such
as .mixWithOthers or .duckOthers, apps define
how they interact with competing streams.
If another app demands exclusive playback (such as a phone call or a
video player), iOS triggers an interruption event
(AVAudioSession.interruptionNotification), pausing the
background stream. Once the interruption ends, the background app
receives a notification allowing it to resume playback
automatically.
Background Control Integration
Continuous background playback is tightly coupled with
MPRemoteCommandCenter and
MPNowPlayingInfoCenter. These frameworks expose transport
controls (play, pause, skip) to the Control Center and the Lock Screen.
By integrating with these controls, the background application can
respond to user inputs without requiring its user interface to be
brought back into the foreground.