Article

Responding to Audio Session Interruptions

Directly observe audio session notifications to ensure that your app responds to interruptions.

Overview

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 AVAudioSession. 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 AVAudioSession 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 AVAudioSessionInterruptionNotification.

func setupNotifications() {
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self,
                                   selector: #selector(handleInterruption),
                                   name: .AVAudioSessionInterruption,
                                   object: nil)
}

@objc func handleInterruption(notification: Notification) {

}

Respond to Interruption Notifications

The posted NSNotification object contains a populated userInfo dictionary providing the details of the interruption. You determine the type of interruption by retrieving the AVAudioSessionInterruptionType value from the userInfo dictionary. The interruption type indicates whether the interruption has begun or has ended.

@objc func handleInterruption(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
            return
    }
    if type == .began {
        // Interruption began, take appropriate actions
    }
    else if type == .ended {
        if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
            let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
            if options.contains(.shouldResume) {
                // Interruption Ended - playback should resume
            } else {
                // Interruption Ended - playback should NOT resume
            }
        }
    }
}

If the interruption type is AVAudioSessionInterruptionTypeEnded, the userInfo dictionary contains an AVAudioSessionInterruptionOptions value, which is used to determine if playback should automatically resume.

See Also

Responding to Audio Session Notifications

Responding to Audio Session Route Changes

Directly observe audio session notifications to ensure that your app responds to interruptions.

AVAudioSessionInterruptionNotification

Posted when an audio interruption occurs.

AVAudioSessionRouteChangeNotification

Posted when the system’s audio route changes.

AVAudioSessionSilenceSecondaryAudioHintNotification

Posted when the primary audio from other applications starts and stops.

AVAudioSessionMediaServicesWereLostNotification

Posted when the media server is terminated.

AVAudioSessionMediaServicesWereResetNotification

Posted when the media server restarts.