قالب وردپرس درنا توس
Home / IOS Development / ios – AVPlayer duration changes when sent to differrent controller

ios – AVPlayer duration changes when sent to differrent controller



I have a controller with an AVPlayer in the compilation cell. When orientation changes to Landscape, the player must get FullScreen.

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

I have tested, even when the orientation is kept the same when the player is passed, the duration will last for a few seconds.

Here is the code:

In ViewController where the cell is present.

  override func viewWillTransition (to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition (to: size, with: coordinator)

guard la videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
leave the player = videoCell.getVideoPlayer
playerViewController = (storyboard! .instantiateViewController (withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
player view controller? .player = player
player view controller? .didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
player view controller? .isMuteTurnedOn = player.isMuted
la varVideoPlaying: Bool = player.isPlaying
current (playerViewController!, animated: false) {
if varVideoPlaying {
player.play ()
}
}
}
otherwise {
videoCell._setMuteIcon ()
videoCell._setPlayPauseIcon ()
player view controller? .dismiss (animated: false, completion: null)
}
}

In FullscreenPlay View Controller

  overrides func viewDidLoad () {
super.viewDidLoad ()
setPlayPauseIcon ()
setMuteIcon ()

let tapGesture = UITapGestureRecognizer (goal: 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 (_ animert: 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 do nothing else in the controller.

Screenshots:
Portrait
  Video Pause in Portrait

LANDSCAPE
  When Rotating to Landscape

When orientation changes, the video moves forward in pause mode.

Thanks for your help in advance.


Source link