Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 i.e 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 Video Paused In Portrait

LANDSCAPE When rotated to landscape

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

Thanks for help in advance.

like image 502
udbhateja Avatar asked Mar 12 '26 04:03

udbhateja


1 Answers

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.

like image 177
jacob bullock Avatar answered Mar 14 '26 22:03

jacob bullock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!