快速处理从 sleep 中唤醒并恢复播放?

标签 swift swift3 tvos apple-tv avplayerviewcontroller

我在 tvOS 上有一个使用 AVPlayerViewController 播放流式音频的应用程序。这工作正常,即使在后台时也是如此。问题是,当 AppleTV 进入休眠状态和唤醒播放器时,播放器会卡住,只有强制退出应用程序才能让我再次开始播放。

我的应用程序是否应该处理某个事件才能正常恢复播放?我应该在文档中寻找什么?

基本代码是:

override func viewDidLoad() {
    super.viewDidLoad()

    // loading details fron plist
    if let stationInfo = readStationSettings() {
        stationUrl = stationInfo["url"] as? String
        stationName = stationInfo["name"] as? String
        stationDescription = stationInfo["description"] as? String
        stationImage = stationInfo["image"] as? UIImage
    } else {
        trackTitle = "Unable to find station details"
        return
    }

    let videoURL = URL(string: stationUrl!)

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation)
    } catch let error as NSError {
        print(error.localizedDescription)
    }

    playerItem = AVPlayerItem(url: videoURL!)

    self.player=AVPlayer(playerItem: playerItem!)

    playerLayer=AVPlayerLayer(player: player)

    self.player?.play()
}

最佳答案

我在使用 iOS iPhone 应用程序时遇到了同样的问题,我认为该解决方案可能会对您有所帮助。我正在使用 AVPlayerViewController 在推送 View Controller 中呈现视频。当视频全屏播放并且应用程序进入休眠状态时,除非您退出应用程序,否则视频永远无法恢复播放。所以这实际上与您遇到的问题相同。

不幸的是,该解决方案涉及调用 AVPlayerViewController 上的私有(private)函数,因为没有其他方法可以以我所知道的任何其他方式退出全屏模式。我的 AppDelegate 持有播放器 View Controller 的一个实例,当 App 退出事件模式时,将调用以下代码:

let appDelegate = UIApplication.getAppDelegate()
if let player = appDelegate.playerController.player {
    if let cl = NSClassFromString("AVFullScreenViewController") {
        for window in UIApplication.sharedApplication().windows {
            if let pvc = window.rootViewController?.presentedViewController {
                if pvc.isKindOfClass(cl) {
                    let dSel = NSSelectorFromString("exitFullScreenAnimated:completionHandler:")
                    if appDelegate.playerController.respondsToSelector(dSel) {
                        appDelegate.playerController.performSelector(dSel, withObject: false, withObject: nil)
                    }
                    break
                }
            }
        }
    }

    player.pause()
    appDelegate.playerController.player = nil
    appDelegate.playerController.removeFromParentViewController()
    appDelegate.playerController.navigationController?.popViewControllerAnimated(false)
}

此代码适用于 Swift 2.3,因此您可能需要将其应用于其他 Swift 或 Objective C 版本。 请注意,这涉及调用私有(private) API 调用,这是一项有风险的业务。 无论如何,我希望这会有所帮助。

另见 this thread在 Apple 开发者论坛上

关于快速处理从 sleep 中唤醒并恢复播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40352987/

相关文章:

ios - 通过 ios 应用程序 (swift) 将 URL 共享到 facebook 后无法获得结果帖子 ID

ios - 如何在 Swift 中生成和处理错误

ios - 在 UICollectionViewCell 中为 UIButton 使用 rx.tap 的问题 - RxSwift 3

swift3 - Swift 3 中 `setImageWith(_:placeholderImage:usingActivityIndicatorStyle:)` 的使用不明确

swift - tvOS:CollectionView 在模拟器上边界完美,但在设备屏幕外

ios - 从 SpriteKit Swift 中的 TimerLabel 中删除多余的零

ios - 火力地堡 swift : queryEqualToValue by childKey is not working

ios - SDWebImage 完成 block

swift - 二元运算符 '==' 不能应用于类型 '[MKAnnotation]' 和 'MKAnnotation' 的操作数

swift - tvOS 上的 UISplitViewController - 禁用滑动到详细 View Controller