Observe audio session notifications to ensure that your app responds appropriately to route changes.
An important responsibility of
AVAudio is managing audio route changes. A route change occurs when the system adds or removes an audio input or output. Route changes occur for several reasons, including a user plugging in a pair of headphones, connecting a Bluetooth LE headset, or unplugging a USB audio interface. When these changes occur, the audio session reroutes audio signals accordingly and broadcasts a notification containing the details of the change to any registered observers.
An important behavior related to route changes occurs when a user plugs in or removes a pair of headphones (see Audio in iOS Human Interface Guidelines). When users connect a pair of wired or wireless headphones, they’re implicitly indicating that audio playback should continue, but privately. They expect an app that’s currently playing media to continue playing without pause. However, when users disconnect their headphones, they don’t want to automatically share what they’re listening to with others. Applications should respect this implicit privacy request and automatically pause playback when users disconnect their headphones.
Observe Route Changes
You can directly observe route change notifications posted by the audio session. This might be useful if you want the system to notify you when a user connects headphones so you can present an icon or message in the player interface.
To observe audio route changes, begin by registering to observe notifications of type
Respond to Route Changes
Notification object contains a populated user-information dictionary providing the details of the route change. Determine the reason for this change by retrieving the
AVAudio value from the dictionary. When a user connects a new device, the reason is
AVAudio, and when a user removes a device, the reason is
When a new device becomes available, you ask the audio session for its
current to determine where the audio output is currently routed. This query returns an
AVAudio object that lists all of the audio session’s inputs and outputs. When the user removes a device, you retrieve the route description for the previous route from the user-information dictionary. In both cases, you query the route description for its outputs, which returns an array of port description objects providing the details of the audio output routes.