4

AVPlayer layer not showing video content sometimes but plays audio. This happens some times not each time Here is my lines of code :

 override func viewDidLoad() {
        super.viewDidLoad()
        self.tempVideoPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tmpMov.mov")
        self.player = AVPlayer(url: self.tempVideoPath!)
}

 override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
     self.playerLayer = AVPlayerLayer(player: self.player)
     self.playerLayer?.frame = self.videoPlayer.bounds
     self.playerLayer?.backgroundColor = UIColor.yellow.cgColor
     self.videoPlayer.layer.addSublayer(playerLayer!)
     print("player = \(playerLayer?.bounds)")

 }

Help me to solve this issue.

Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51

1 Answers1

2

the problem with you code is that the bounds of videoPlayer can change. So you need to add the following code to your view controller to change AVPlayerLayer's frame accordingly:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    playerLayer?.frame = view.bounds
}

Hope it helps.

jajo
  • 21
  • 2
  • 1
    Thanks this was the missing piece I was looking for. If the audio for your video is playing but the video is not showing this helps. – Mark Thomas Jan 06 '23 at 01:06