Luciano Perez
Luciano Perez

Reputation: 91

Playback buttons do not appear in an AVPlayer after adding a layer - iOS 16

I am using an AVPlayer to present a video. The app has only one .mp4 but for a different use case, the same video needs to get flipped.

The buttons are there and completely functional, you can press the play and the 15 seconds forward/backward buttons but they don't appear on the screen (4th video in the attached image)

The issue seems to be that the flip layer I am adding overlays the new layout buttons.

The potential fix I was thinking of is to flip the video before adding it to the player.

Do you know if there is a straightforward solution for this? Maybe there is an easy way to keep the iOS 15 playback button layout?

enter image description here

The code the app is using to flip the video is as follows:

  @IBAction func pressButton(_ sender: Any) {
    guard let path = Bundle.main.path(forResource: "sample-5s", ofType:"mp4") else {
       return
    }
    let avPlayer = AVPlayer(url: URL(fileURLWithPath: path))
    let avPlayerController = AVPlayerViewController()
    
    present(avPlayerController, animated: true, completion: {
   
      let flippedLayer = AVPlayerLayer(player: avPlayer)
      let transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
      flippedLayer.frame = (avPlayerController as UIViewController).view.frame
      flippedLayer.setAffineTransform(transform)
      (avPlayerController as UIViewController).view.layer.addSublayer(flippedLayer)
     
      avPlayerController.player = avPlayer
      avPlayer.play()
    })
  }

Upvotes: 2

Views: 3469

Answers (2)

For Swift UI

CustomVideoPlayer()
   .compositingGroup()




struct CustomVideoPlayer : UIViewControllerRepresentable {

 var player: AVPlayer

 func makeUIViewController(context: UIViewControllerRepresentableContext<CustomVideoPlayer>) -> AVPlayerViewController {
     
     let controller = AVPlayerViewController()
     controller.player = player
     return controller

 }

 func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<CustomVideoPlayer>) {
 }

Fixed problem for me in IOS 16 Swift UI

Upvotes: 0

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

Found a solution. It's all about adding AVPlayerViewController inside the parent view controller. Assume that you have a AVPlayerViewController inside a viewController. So first in viewDidLoad make sure to add it as a child.

var playerController = AVPlayerViewController()
lazy var playerBaseView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
}()

override func viewDidLoad() {
    // Make your player parent view constraints or do it on Storyboard
    playerBaseView.snp.makeConstraints { (make) in
       make.edges.equalTo(view.safeAreaLayoutGuide.snp.edges)
    }

   addChild(playerController) // Add as a child
   playerBaseView.addSubview(playerController.view)

   // Make your playerController view constraints or do it on Storyboard
   playerController.view.snp.makeConstraints { (make) in
       make.edges.equalToSuperview()
   }
}

Upvotes: 2

Related Questions