gpt4 book ai didi

ios - 传递给不同 Controller 时 AVPlayer 持续时间会发生变化

转载 作者:搜寻专家 更新时间:2023-10-31 22:37:42 26 4
gpt4 key购买 nike

我在 Collection View 单元格中有一个带有 AVPlayer 的 Controller 。当方向更改为横向时,播放器应获得全屏。

为此,我在 Collection View Cell 中展示了一个具有相同播放器实例的 AVPlayerController。在播放模式下旋转视频时效果很好。但是,当视频暂停并且我将方向更改为横向时,当前帧会发生变化,即视频向前移动。

我已经测试过,即使方向保持不变,当玩家经过时,持续时间也会跳过几秒钟。

代码如下:

在存在单元格的 ViewController 中。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}

在全屏播放器 View Controller 中

override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}

我在 Controller 中什么都不做。

截图:纵向 Video Paused In Portrait

景观 When rotated to landscape

当方向改变时,即使在暂停状态下视频也会向前移动。

提前感谢您的帮助。

最佳答案

您应该 try catch currentTime,然后在播放器上使用 seek(to time: CMTime) 方法以在该确切时间开始播放。我不确定为什么这会发生在你身上,但我认为这会让你得到你正在寻找的结果。

关于ios - 传递给不同 Controller 时 AVPlayer 持续时间会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394544/

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