How to Detect Fullscreen Mode Using Avplayerviewcontroller in Swift

How to detect fullscreen mode using AVPlayerViewController in Swift?

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

Handle when AVPlayerViewController goes full screen

The answer is here suggesting to observe the videoBounds of the AVPlayerViewController that I initially observed but did not work for me.

This solution is not always working. The notification related to the videoBounds property of the AVPlayerViewController controller is not always issued.



Related Topics



Leave a reply



Submit