我在集合视图单元格中有一个带有AVPlayer的控制器。当方向更改为横向时,播放器应获得全屏显示。
为此,我在Collection View Cell中展示了一个具有Player实例的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)
}
}
在FullscreenPlayer 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)
}
我在控制器中什么也没做。
当方向改变时,即使在暂停状态下,视频也会前进。
谢谢您的帮助。
答案 0 :(得分:2)
您应该尝试捕获currentTime
,然后在播放器上使用seek(to time: CMTime)
方法在该确切时间开始。我不确定为什么会发生这种情况,但是我认为这会为您带来所需的结果。