Directly observe audio session notifications to ensure that your app responds to interruptions.
Interruptions are a common part of the iOS user experience. For example, consider what happens if you’re watching a movie in the Videos app and you receive a phone call or FaceTime request. In this scenario, your movie’s audio fades out, playback pauses, and the sound of the ringtone fades in. If you decline the call or request, control returns to the Videos app, and playback begins again as the movie’s audio fades in.
At the center of this behavior is your app’s
AVAudio. As interruptions begin and end, it notifies any registered observers so they can take the appropriate action.
AVPlayer monitors your audio session and automatically pauses and resumes playback in response to
AVAudio interruption events. To observe this
AVPlayer behavior, use key-value observing (KVO) on the player’s rate property so you can update your user interface as the player pauses and resumes in response to interruptions.
Observe for Interruption Notifications
You can also directly observe interruption notifications posted by AVAudioSession. This might be useful if you'd like to know if playback was paused because of an interruption or another reason, such as a route change. To observe audio interruptions, begin by registering to observe notifications of type
Respond to Interruption Notifications
NSNotification object contains a populated
user dictionary providing the details of the interruption. You determine the type of interruption by retrieving the
AVAudio value from the
user dictionary. The interruption type indicates whether the interruption has begun or has ended.
If the interruption type is
user dictionary contains an
AVAudio value, which is used to determine if playback should automatically resume.