Observe audio session notifications to ensure that your app responds appropriately to interruptions.
Interruptions are a common part of the iOS and watchOS user experiences. For example, consider the case of receiving a phone call while you’re watching a movie in the TV app on your iPhone. In this scenario, the movie’s audio fades out, playback pauses, and the sound of the call’s ringtone fades in. If you decline the call, control returns to the TV app, and playback begins again as the movie’s audio fades in.
At the center of this behavior is your app’s audio session. As interruptions begin and end, the audio session notifies any registered observers so they can take appropriate action. For example,
AVPlayer monitors your app’s audio session and automatically pauses and resumes playback in response to interruption events. You can observe these changes by key-value observing the player’s
rate property and updating your user interface as needed when the player pauses and resumes playback.
You can directly observe interruption notifications posted by
AVAudio. This might be useful if you’d like to know if the system paused playback due to an interruption or another reason, such as a route change. To observe audio interruptions, begin by registering to observe notifications of type
Respond to Interruptions
Notification object contains a populated user-information 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 ended.
If the interruption type is
user dictionary contains an
AVAudio value, which you use to determine if playback should automatically resume.