首页 文章

AVPlayerViewController在纵向应用程序中的全屏旋转行为

提问于
浏览
2

当在仅限肖像的iOS应用程序中嵌入AVPlayerViewController时,如果视频在横向保持设备的情况下进行全屏筛选,则当播放器退出全屏时,应用程序可能会陷入奇怪的布局 .

这是一个错误还是我做错了什么?

以下是如何使用Xcode 9.4.1,swift 4,iOS 11.4,模拟器或物理设备重现干净的项目 .

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    //Create the player and add as child view controller
    let playerVC = AVPlayerViewController()
    self.addChildViewController(playerVC)

    //Place player's view in self
    playerVC.view.frame = CGRect(x: 10, y: 40, width: 355, height: 200)
    self.view.addSubview(playerVC.view)

    //Load example video
    playerVC.player = AVPlayer(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!)
}

它是如何正常工作的:

  • 播放视频,点击全屏

  • 旋转到横向,视频旋转

  • 关闭全屏,无论屏幕或设备方向如何,应用都会返回纵向

  • ex:https://imgur.com/a/MPFmzyH

它如何打破:

  • 播放视频,将设备旋转到横向(屏幕不旋转)

  • 点击全屏

  • 退出全屏

  • 屏幕中断,旋转无法修复

  • ex:https://imgur.com/a/hDdmu20

2 回答

  • 4

    当您将播放器全屏显示时,请输入View Controller的viewWillApper . 所以在viewWillAppear中尝试将窗口框架设置为等于屏幕边界 .

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        appDelegate.window?.rootViewController?.view.frame = UIScreen.main.bounds
    }
    
  • 0

    您可以识别播放器何时成为全屏并强制为视图控制器设置纵向或横向 .

    它不是最好的解决方案,但至少可以避免破坏UI:

    import AVFoundation
        import AVKit
    
        var isPlayerFullscreen: Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Create the player and add as child view controller
            let playerVC = AVPlayerViewController()
            self.addChildViewController(playerVC)
    
            //Place player's view in self
            playerVC.view.frame = CGRect(x: 10, y: 40, width: 355, height: 200)
            self.view.addSubview(playerVC.view)
    
            //Load example video
            playerVC.player = AVPlayer(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!)
    
            //Add an observer for playerVC object
            playerVC.addObserver(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil)
        }
    
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
        {
            if keyPath == "videoBounds", let rect = change?[.newKey] as? NSValue
            {
                let playerRect: CGRect = rect.cgRectValue
                if playerRect.size.height <= 200 {
                    print("Video not in full screen")                
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
    isPlayerFullscreen = false
                } else {
                    print("Video in full screen")
                    isPlayerFullscreen = true
                }
            }
        }
    
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return isPlayerFullscreen ? UIInterfaceOrientationMask.landscape: UIInterfaceOrientationMask.portrait
        }
    
        override var shouldAutorotate: Bool {
            return true
        }
    

相关问题