Directly observe audio session notifications to ensure that your app responds to interruptions.
An important responsibility of
AVAudio is managing audio route changes. A route change occurs when an audio input or output is added to or removed from an iOS device. Route changes occur for a number of 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,
AVAudio 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
Sound in iOS Human Interface Guidelines). When users connect a pair of wired or wireless headphones, they are implicitly indicating that audio playback should continue, but privately. They expect an app that is currently playing media to continue playing without pause. When users unplug their headphones, they don’t want to automatically share what they are listening to with others. Applications should respect this implicit privacy request and automatically pause playback when headphones are removed.
AVPlayer monitors your app’s audio session and responds appropriately to route changes. When headphones are connected, playback continues as expected. When headphones are removed, playback is automatically paused. To observe this
AVPlayer behavior, use key-value observing (KVO) on the player’s
rate property so that you can update your user interface as the player is paused in response to an audio route change.
Observe for Route Change Notifications
You can also directly observe any route change notifications posted by
AVAudio. This might be useful if you want to be notified when a user connects or removes 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 Change Notifications
NSNotification object contains a populated
user dictionary providing the details of the route change. Determine the reason for this change by retrieving the
AVAudio value from the
user dictionary. When a new device is connected, the reason is
AVAudio, and when a value is removed, the reason is
When a new device becomes available, you ask the
AVAudio for its
current to determine where the audio output is currently routed. This query returns an
AVAudio listing all of the audio session’s inputs and outputs. When a device is removed, you retrieve the
AVAudio for the previous route from the
user dictionary. In both cases, you query the route description for its outputs, which returns an array of
AVAudio objects providing the details of the audio output routes.