ios - 如何在 iOS 中将电话应用程序发送到后台

标签 ios avplayer nsurlsession avplayerviewcontroller

在我的申请中,我有一个要求。我正在运行我的应用程序并使用 AVPlayerViewcontroller 播放一个视频。在中间我接到电话,我接听了电话,我的视频将暂停。5秒后,我将从服务器获取新的网址以在 AVPlayerViewcontroller 中播放。此时新网址在后台播放,可以听到电话调用的声音。在这种情况下,我想将手机应用程序发送到后台,并希望查看 avplayer 中正在播放的视频。

请告诉我是否有任何方法可以实现这一目标。

最佳答案

SWIFT:

  1. Observe for Interruption Notifications
func setupNotifications() {
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self,
                                   selector: #selector(handleInterruption),
                                   name: .AVAudioSessionInterruption,
                                   object: nil)
}
  1. Respond to Interruption Notifications
@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
            }
        }
    }
}

Apple documentation

关于ios - 如何在 iOS 中将电话应用程序发送到后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922608/

相关文章:

swift - 如何从 NSURLSession.sharedSession().dataTaskWithRequest 获取数据

ios - 请求时间过长时取消 NSURLSession dataTask

ios - 我可以在 UIAlertView 的按钮中使用多行文本吗?

notifications - MPMusicPlayerController/AVPlayer、iTunes 匹配和通知

ios - 使用 AVFoundation 在视频之间快速切换

ios - AVPlayer 中的洗涤器(UISlider)?

ios - 使用 RxSwift 从 UITableViewCell 发送回调到 UIViewController

ios - 应用程序委托(delegate)的 Swift Xcode 7 错误

ios - 用于标记 Google Maps SDK、Swift 2.0 的自定义信息窗口

ios - 如何使用 NSURLSession 复制 POST 登录