iOS Swift无法解雇avplayer模态

时间:2017-08-30 08:36:32

标签: ios swift video avplayer

所以这是我的用例 - 我们正在使用avplayer加载视频并播放它,用户可以点击默认的全屏按钮来全屏播放视频。用户可能正在以两种不同的条件观看视频,如果他已登录并且他未登录。

如果用户已登录(由变量值确定),他可以观看完整视频,否则播放应在播放定义的秒数后停止(取决于视频的秒数变化),以及横幅出现在要求他登录的玩家身上。

视频正在内联播放时,一切正常。但是,当视频正在全屏播放时,即使我们使用didTapPause()停止播放,全屏窗口也不会被解除。我甚至尝试使用self.playerController.dismiss(animated: true, completion: nil)解雇它,全屏模式不会被解雇。代码片段如下 -

playerController.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
        if self.playerController.player?.currentItem?.status == .readyToPlay {
            self.videoCurrentTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem!.currentTime())!);
            self.videoTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem?.duration)!);



            if self.moveToTime != nil{
                let timeWithSecond = CMTimeMakeWithSeconds(self.videoTimeDuration! * self.moveToTime! / 100, Int32(kCMTimeMaxTimescale))

                self.playerController.player?.seek(to: timeWithSecond, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
                self.moveToTime = nil
            }

            guard let videoD = self.videoData else { return }

            let timeTToPlay: Double = Double(videoD.freeDuration)

            if videoD.isFree {
                if videoD.registrationNeeded && !CurrentLoginUser.shared.isLogin{
                    if self.videoCurrentTimeDuration! > timeTToPlay {
                        self.didTapPause()

                        self.playerController.dismiss(animated: true, completion: nil)


                        self.loginNeedView = UINib.get(withNib: self)
                        self.loginNeedView?.frame = self.bounds

                        self.loginNeedView?.autoresizingMask = [
                            UIViewAutoresizing.flexibleWidth,
                            UIViewAutoresizing.flexibleHeight
                        ]
                        self.addSubview(self.loginNeedView!)
                        AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
                    }
                }
                else{
                    self.loginNeedView?.removeFromSuperview()
                    AppUtility.lockOrientation(UIInterfaceOrientationMask.all)
                }
            }

通过调用setupView函数将播放器控制器添加到视图中,如下所示 -

private func setUpView() {
    self.backgroundColor = .black
    addVideoPlayerView()
    configurateControls()
}

fileprivate func addVideoPlayerView() {
    playerController.view.frame = self.bounds
    playerController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    playerController.showsPlaybackControls = true
    playerController.addObserver(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil)
    self.insertSubview(playerController.view, at: 0)
}

我不确定这是否是正确的方法,任何想法?

编辑:

接下来按照anbu.karthik的建议,我试图通过将顶视图控制器定位为强制删除全屏视图:

func currentTopViewController() -> UIViewController {
var topVC: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController
while ((topVC?.presentedViewController) != nil) {
topVC = topVC?.presentedViewController
}
return topVC! 
}

然后,按如下方式使用 -

let currentTopVC: UIViewController? = self.currentTopViewController()

                        if (currentTopVC?.description.contains("AVFullScreenViewController"))! {
                            print("found it")

                            currentTopVC?.dismiss(animated: true) { _ in }
                        }

它有效,但崩溃应用程序时出现以下异常 -

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<AVEmbeddedPlaybackControlsViewController: 0x7fdcc2264400> should have parent view controller:<AVPlayerViewController: 0x7fdcc222a800> but actual parent is:<AVFullScreenViewController: 0x7fdcce2f3c50>'

1 个答案:

答案 0 :(得分:1)

据我所知,不可能直接全屏播放。看起来AVPlayerViewController几乎就是这样,并没有为UI或行为的自定义提供太多。如果你想直接全屏播放,你需要以全屏模式呈现包含AVPlayerViewController的控制器,或者自己更换帧以使其全屏显示。但是没有API可以在AVPlayerViewController上以编程方式控制全屏...