【发布时间】:2017-08-30 08:36:31
【问题描述】:
所以这是我的用例 - 我们正在使用 avplayer 加载视频并播放它,用户可以单击默认的全屏按钮来全屏播放视频。用户可能在 2 种不同的条件下观看视频,如果他已登录,如果他未登录。
如果用户已登录(由变量值确定),他可以观看完整视频,否则,播放应在播放定义的秒数后停止(取决于视频的秒数变化),并且横幅出现在玩家上方,要求他登录。
在线播放视频时一切正常。但是,当视频全屏播放时,即使我们使用didTapPause() 停止播放,全屏窗口也不会被关闭。我什至尝试使用self.playerController.dismiss(animated: true, completion: nil) 将其关闭,全屏模式不会被关闭。代码sn -p 如下-
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>'
【问题讨论】:
-
你没有被删除
addPeriodicTimeObserver