How to detect fullscreen mode using AVPlayerViewController in Swift?

前端 未结 5 579
花落未央
花落未央 2020-12-10 18:12

I am trying to detect when the AVPlayerViewController is in full-screen mode, but I\'m having a difficult time achieving this. I\'d like to know when the user s

5条回答
  •  生来不讨喜
    2020-12-10 19:05

    Updated for Swift 3:

    Add an observer for the playerViewController object:

    playerViewController(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"
        {
            if let rect = change?[.newKey] as? NSValue
            {
                if let newrect = rect.cgRectValue as CGRect?
                {
                    // 200 is height of playerViewController in normal screen mode
                    if newrect.size.height <= 200
                    {
                        print("normal screen")
                    }
                    else
                    {
                        print("full screen")
                    }
                }
            }
        }
    }
    

提交回复
热议问题