简体   繁体   中英

AVPlayer playing video sideways in Swift

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM