我在集合视图单元格中有一个带有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)
    }

我在控制器中什么也没做。

屏幕截图:
肖像
ios - AVPlayer持续时间在传递给其他 Controller 时发生变化-LMLPHP

景观
ios - AVPlayer持续时间在传递给其他 Controller 时发生变化-LMLPHP

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

预先感谢您的帮助。

最佳答案

您应该尝试捕获currentTime,然后在播放器上使用seek(to time: CMTime)方法在该确切时间启动。我不确定为什么会发生这种情况,但是我认为这会为您带来所需的结果。

07-26 09:37
查看更多