iosvideoswift3autorotate

swift 3 - play video in fullscreen when rotated to landscape


I have a container view that plays the video in it when device is portrait. I want the video to play fullscreen automatically when device is rotated. I've tried this code that detects when device is rotated but it doesn't change the container to full screen

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {

        videoContainerView.frame = self.view.frame
       // self.videoContainerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) // this didn't work to....
        print("Device is landscape")
    }
    else{
        print("Device is portrait")
    }

}

The video is on an avplayer player. thank's


Solution

  • O.k. so i managed to play the video fullscreen by adding the video to an AVPlayerLayer and setting it's size to the view when Device is landscape.

    override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    
        if (UIDevice.current.orientation.isLandscape) {
    
                        DispatchQueue.main.async {
                           self.view.didAddSubview(self.controllsContainerView)
                           self.layer = AVPlayerLayer(player: self.player!)
                            self.layer?.frame = self.view.frame
                            self.layer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                            self.view.layer.addSublayer(self.layer!)
    
            }
            print("Device is landscape")
        }
    

    when UIDevice.cuurent.orientation.islandscape == false i remove subLayer and set the view back to videoContainer bounds.

    else{
            print("Device is portrait")
            movie.view.frame = videoContainerView.bounds
            controllsContainerView.frame = videoContainerView.bounds
            self.layer?.removeFromSuperlayer()
    
    
        }
    

    hope it helps anyone.