AVPlayer not full screen in landscape mode using iOS swift

别来无恙 提交于 2019-12-09 20:42:19

问题


I have a video view set to full screen. However while playing in the simulator, it is not running full screen.

This issue is only for iPads and not iPhones.

Here is my code:

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(true)

    let bundle: Bundle = Bundle.main
    let videoPlayer: String = bundle.path(forResource: "normalnewer", ofType: "mp4")!
    let movieUrl : NSURL = NSURL.fileURL(withPath: videoPlayer) as NSURL
    //        var fileURL = NSURL(fileURLWithPath: "/Users/Mantas/Desktop/123/123/video-1453562323.mp4.mp4")
    playerView = AVPlayer(url: movieUrl as URL)

    NotificationCenter.default.addObserver(self,selector: #selector(playerItemDidReachEnd),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: self.playerView.currentItem) // Add observer

            var playerLayer=AVPlayerLayer(player: playerView)
//        self.avPlayerLayer.playerLayer = AVLayerVideoGravityResizeAspectFill


    playerLayer.frame = viewVideo.bounds
//        self.playerLayer.frame = self.videoPreviewLayer.bounds

    viewVideo.layer.addSublayer(playerLayer)




    playerView.play()

    UserDefaults.standard.set("normalnewer", forKey: "video")




}

I have tried checking landscape mode in target's general. Gone through the threads below: how to play a video in fullscreen mode using swift ios?

Play video fullscreen in landscape mode, when my entire application is in lock in portrait mode

Setting device orientation in Swift iOS

But these did not resolve my issue. Here is a screenshot.

Can somebody help me resolve this?


回答1:


I see you solved your issue, but I noticed you were using an AVPlayerLayer. The orientation rotation is handled with the AVPlayerViewController, but not with a custom view using a player layer. It is useful to be able to put the layer in fullscreen without rotating the device. I answered this question elsewhere, but I will put it here as well.

Transforms and frame manipulation can solve this issue:

extension CGAffineTransform {

    static let ninetyDegreeRotation = CGAffineTransform(rotationAngle: CGFloat(M_PI / 2))
}

extension AVPlayerLayer {

    var fullScreenAnimationDuration: TimeInterval {
        return 0.15
    }

    func minimizeToFrame(_ frame: CGRect) {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.setAffineTransform(.identity)
            self.frame = frame
        }
    }

    func goFullscreen() {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.setAffineTransform(.ninetyDegreeRotation)
            self.frame = UIScreen.main.bounds
        }
    }
}

Setting the frame of the AVPlayerLayer changes it's parent's frame. Save the original frame in your view subclass, to minimize the AVPLayerLayer back to where it was. This allows for autolayout.

IMPORTANT - This only works if the player is in the center of your view subclass.

Incomplete example:

class AVPlayerView: UIView {

    fileprivate var avPlayerLayer: AVPlayerLayer {
        return layer as! AVPlayerLayer
    }

    fileprivate var hasGoneFullScreen = false
    fileprivate var isPlaying = false
    fileprivate var originalFrame = CGRect.zero

    func togglePlayback() {
        if !hasGoneFullScreen {
            originalFrame = frame
            hasGoneFullScreen = true
        }

        isPlaying = !isPlaying
        if isPlaying {
            avPlayerLayer.goFullscreen()
            avPlayerLayer.player?.play()
        } else {
            avPlayerLayer.player?.pause()
            avPlayerLayer.minimizeToFrame(originalFrame)
        }
    }
}



回答2:


After spending some time taking a good look at the Storyboard, I tried changing the fill to Aspect fill & fit and that resolved the problem :)



来源:https://stackoverflow.com/questions/43137133/avplayer-not-full-screen-in-landscape-mode-using-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!