Article

Responding to Audio Session Interruptions

Observe audio session notifications to ensure that your app responds appropriately to interruptions.

Overview

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.

Observe Interruptions

You can directly observe interruption notifications posted by AVAudioSession. 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 interruptionNotification.

func setupNotifications() {
    // Get the default notification center instance.
    let nc = NotificationCenter.default
    nc.addObserver(self,
                   selector: #selector(handleInterruption),
                   name: AVAudioSession.interruptionNotification,
                   object: nil)
}

@objc func handleInterruption(notification: Notification) {
    // To be implemented.
}

Respond to Interruptions

The posted Notification object contains a populated user-information dictionary providing the details of the interruption. You determine the type of interruption by retrieving the AVAudioSession.InterruptionType value from the userInfo dictionary. The interruption type indicates whether the interruption has begun or ended.

@objc func handleInterruption(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
            return
    }

    // Switch over the interruption type.
    switch type {

    case .began:
        // An interruption began. Update the UI as needed.

    case .ended:
       // An interruption ended. Resume playback, if appropriate.

        guard let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
        let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
        if options.contains(.shouldResume) {
            // Interruption ended. Playback should resume.
        } else {
            // Interruption ended. Playback should not resume.
        }

    default: ()
    }
}

If the interruption type is AVAudioSession.InterruptionType.ended, the userInfo dictionary contains an AVAudioSession.InterruptionOptions value, which you use to determine if playback should automatically resume.

See Also

Responding to Audio Session Notifications

Responding to Audio Session Route Changes

Observe audio session notifications to ensure that your app responds appropriately to route changes.

class let interruptionNotification: NSNotification.Name

A notification that’s posted when an audio interruption occurs.

class let routeChangeNotification: NSNotification.Name

A notification that’s posted when the system’s audio route changes.

class let silenceSecondaryAudioHintNotification: NSNotification.Name

A notification that’s posted when the primary audio from other applications starts and stops.

class let mediaServicesWereLostNotification: NSNotification.Name

A notification that’s posted when the system terminates the media server.

class let mediaServicesWereResetNotification: NSNotification.Name

A notification that’s posted when the media server restarts.