通过GPS应用程序意味着检测AVPlayer背景干扰Means to Detect AVPlayer Background Interruption by GPS Application

- 此内容更新于:2015-01-27
主题:

原文:

I'm teaching myself Swift by writing a Podcast player. When Siri interrupts my app, the interruption handler is called and behaves nicely:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    ...
    // set up audio session
    var asError: NSError?
    self.audioSession = AVAudioSession.sharedInstance()
    self.audioSession.setCategory(AVAudioSessionCategoryPlayback, error: &asError)
    self.audioSession.setActive(true, withOptions: AVAudioSessionSetActiveOptions.OptionNotifyOthersOnDeactivation, error: &asError)
    self.notificationCenter.addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: self.audioSession)
    ...
}

The handler is this (mostly copied from examples):

// handle audio interruption
func playInterrupt(notification: NSNotification) {

    if notification.name == AVAudioSessionInterruptionNotification
        && notification.userInfo != nil {

            var info = notification.userInfo!
            var intValue: UInt = 0
            (info[AVAudioSessionInterruptionTypeKey] as NSValue).getValue(&intValue)

            if let type = AVAudioSessionInterruptionType(rawValue: intValue) {

                switch type {

                case .Began:
                    // interruption began
                    // NOTE: the pause function saves play state
                    self.podPlayer.pause()

                case .Ended:
                    // interruption ended

                    if let option: AnyObject = notification.userInfo?[AVAudioSessionInterruptionOptionKey] {

                        if let option = option as? UInt {
                            let options = AVAudioSessionInterruptionOptions(option)

                            if options == AVAudioSessionInterruptionOptions.OptionShouldResume {
                                // backup up a few seconds after interruption

                                if self.podPlayer.currentPlaybackTime > const.resumeInterval {
                                    let backup = self.podPlayer.currentPlaybackTime - const.resumeInterval
                                    self.podPlayer.seekToTime(CMTimeMake(Int64(backup), const.timeScale))
                                }

                                self.podPlayer.play()
                            } // end if the options include option should resume
                        } // end if the options are valid
                    } // end if there are any interruption options
                } // end switch on interruption type
            } // end if there is a valid interruption type
    } // end if the notification is AVAudioSessionInterruptionNotification and there is userInfo data
} // end playInterrupt

The issue is that my GPS application (which shall remain unnamed) doesn't cause that interrupt. I'd like to have the same backup and resume behavior I get with Siri.

So how can I detect when another application overlays audio on my playback?