0

I am trying to record a video and then play it back, the problem is when the video is played back, it plays sideways. I've disabled landscape mode so I have no idea what could be causing the issue. Here's the implementation:

class VideoPlayback: UIViewController {

    let avPlayer = AVPlayer()
    var avPlayerLayer: AVPlayerLayer!

    var videoURL: URL!
    //connect this to your uiview in storyboard
    @IBOutlet weak var videoView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer.frame = view.frame
        avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoView.layer.insertSublayer(avPlayerLayer, at: 0)
        view.layoutIfNeeded()
        
        let playerItem = AVPlayerItem(url: videoURL as URL)
        avPlayer.replaceCurrentItem(with: playerItem)
    
        avPlayer.play()
    }
}

Anyone know how to fix this?

Noah Iarrobino
  • 1,435
  • 1
  • 10
  • 31
  • Please look at my answer https://stackoverflow.com/questions/46243096/exporting-time-lapse-with-avassetexportsession-results-in-black-video/63348323#63348323 – iUrii Sep 01 '20 at 09:28

1 Answers1

2
let angle = .pi/2    //90 degrees -- or desired rotation
avPlayerLayer.setAffineTransform(CGAffineTransform(rotationAngle: angle)

This should work for that one video, but if you are going to play different videos it may not be reliable.

nick
  • 107
  • 4