User guide

In the figure, the system initially determines the audio route after your app launches. It continues to monitor
the active route as your app runs. Consider first the case of a user tapping a Record button in your app,
represented by the “Recording starts” box on the left side of the figure.
During recording, the user may plug in or unplug a headset—see the diamond-shaped decision element
toward the lower-left of the figure. In response, the system sends
AVAudioSessionRouteChangeNotification containing the reason for the change and the previous route.
Your app should stop recording.
The case for playback is similar but has different outcomes, as shown on the right of the figure. If a user unplugs
the headset during playback, your app should pause the audio. If a user plugs in the headset during playback,
your app should simply allow playback to continue.
The AddMusic sample code project demonstrates how to implement the playback portion of this behavior.
Responding to Audio Hardware Route Changes
There are two parts to configuring your app to respond to route changes:
1.
Implement methods to be invoked upon a route change.
2.
Register for the AVAudioSessionRouteChangeNotification notification to respond to route changes.
For example, your app receives a notification when a user unplugs the headset during playback. Following
Apple guidelines, your app pauses. Your app can then provide a display that prompts the user to continue
playing.
When the system sends a route-change notification, it provides the information you need to figure out which
action to take. Register for the AVAudioSessionRouteChangeNotification notification as shown:
NSNotificationCenter *nc [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:routeChanged:
name:AVAudioSessionRouteChangeNotification
object:nil];
After the notification is received, your app calls the method you designate and changes its behavior based on
the information contained by the notification. The AVAudioSessionRouteChangeNotification contains
a userInfo dictionary that describes:
Why the route changed
Responding to Route Changes
Responding to Audio Hardware Route Changes
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
38