AVPlayer playing video sideways in Swift

261 Views Asked by At

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?

1

There are 1 best solutions below

0
On
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.