简体   繁体   中英

AVPlayer duration changes when passed to differrent controller

I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.

For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode. However, when video is paused and I change orientation to Landscape, the frame at current moment changes ie video moves forward.

I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.

Here is the code:

In ViewController where cell is present.

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)
    }
}

In 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)
    }

I am doing nothing else in controller.

Screenshots: Portrait 视频暂停在肖像中

LANDSCAPE 旋转到横向时

When orientation changes, video moves forward even on pause state.

Thanks for help in advance.

You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM