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

标签 ios swift video avplayer avplayerviewcontroller

我在 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/

相关文章:

ios - 如何更改 userInterfaceLayoutDirection

swift - 快速从单元格中选择单个选择

android - 在android中检测录制视频的方向

audio - 在ffmpeg中将附加音频文件与视频(+音频)混合

c++ - CvVideoCamera processImage 不会改变图像

ios - 设置 UI 时 Swift 在 `init` 中看似奇怪的行为

ios - 如何拥有相同更新的对象的多个副本,同时在不同 View Controller 中实例化为单独的对象?

ios - 核心图 : Label in BarChart

iOS - 多播网络失败 : "Can' t assign requested address"

ios - 我们如何将数据写入 firebase 实时数据库?尽管启用了读/写,但似乎不起作用