iOS Swift不能关闭avplayer模式
问题描述:
所以这里是我的使用案例 - 我们正在使用avplayer加载视频并播放它,用户可以点击默认的全屏按钮以全屏模式播放视频。如果用户已登录并且未登录,用户可能会在两种不同的条件下观看视频。iOS Swift不能关闭avplayer模式
如果用户已登录(由变量值确定),则他可以观看完整的视频,否则,播放应在播放指定的秒数后停止(取决于视频中秒数的变化),并在播放器上出现横幅,要求其登录。
内联播放视频时,一切正常。但是,当视频播放全屏时,即使我们使用didTapPause()
停止播放,全屏窗口也不会被解除。我甚至尝试使用self.playerController.dismiss(animated: true, completion: nil)
解雇它,全屏模式并没有被解雇。代码片段是如下 -
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)
}
我不知道这是否是正确的方法 -
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
功能,如下所示添加到视图做到这一点,有什么想法?
编辑:
接着按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>'
答
据我所知,不可能直接播放全屏。看起来,AVPlayerViewController几乎是一样的,并没有提供很多定制UI或行为的方式。如果您想直接播放全屏,则需要以全屏模式呈现包含AVPlayerViewController的控制器,或者自行更改框架以使其全屏显示。但是没有API在AVPlayerViewController上以编程方式控制全屏...
您没有删除'addPeriodicTimeObserver' –