gpt4 book ai didi

iOS Swift 无法关闭 avplayer 模式

转载 作者:行者123 更新时间:2023-11-28 15:21:16 27 4
gpt4 key购买 nike

这是我的用例 - 我们正在使用 avplayer 加载视频并播放它,用户可以单击默认的全屏按钮来全屏播放视频。用户可能会在 2 种不同的情况下观看视频,一种是登录状态,另一种是未登录状态。

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

在线播放视频时一切正常。但是,当视频全屏播放时,即使我们使用 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 函数将播放器 Controller 添加到 View 中,如下所示 -

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 的建议,我尝试通过将顶 View Controller 定位为强行删除全屏 View :

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 的 Controller ,或者自己更改框架以使其全屏显示。但是没有 API 可以在 AVPlayerViewController 上以编程方式控制全屏...

关于iOS Swift 无法关闭 avplayer 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com