AVPlayer在使用iOS swift的横向模式下不是全屏

时间:2017-03-31 09:25:57

标签: ios iphone swift avplayer

我将视频视图设置为全屏。但是,在模拟器中播放时,它不是全屏运行。

此问题仅适用于iPad而不适用于iPhone。

这是我的代码:

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")




}

我已尝试检查目标常规中的横向模式。 通过下面的主题: 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

但这些并没有解决我的问题。 这是一个截图。 Screenshot of the error

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

我看到你解决了你的问题,但我注意到你使用的是AVPlayerLayer。使用AVPlayerViewController处理方向旋转,但不使用播放器层处理自定义视图。能够在不旋转设备的情况下将图层全屏放置是有用的。我在其他地方回答了这个问题,但我也会把它放在这里。

变换和帧操作可以解决这个问题:

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
        }
    }
}

设置AVPlayerLayer的帧会更改其父级的帧。将原始帧保存在视图子类中,以将AVPLayerLayer最小化回原位。这允许自动布局。

重要信息 - 仅当播放器位于视图子类的中心时才有效。

不完整的例子:

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)
        }
    }
}

答案 1 :(得分:1)

花了一些时间仔细观察故事板后,我尝试将填充更改为Aspect fill&适合并解决了问题:)