Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect fullscreen mode using AVPlayerViewController in Swift?

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 selects the expand button to enter full screen as shown here:

enter image description here

I've added the appropriate observer per these suggestions:

  1. Detect Video playing full screen in Portrait or landscape
  2. How to detect fullscreen mode of AVPlayerViewController

The appropriate code:

var avWidth:CGFloat = 375
var avHeight:CGFloat = 300

override func viewDidLoad()
{
    super.viewDidLoad()

    let path = NSBundle.mainBundle().pathForResource("cable pressback", ofType: "mp4")
    let url = NSURL.fileURLWithPath(path!)
    let player = AVPlayer(URL: url)

    playerViewController.player = player

    playerViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, 300)

    playerViewController.view.translatesAutoresizingMaskIntoConstraints = true

    view.addSubview(playerViewController.view)

    self.addChildViewController(playerViewController)

    [playerViewController .addObserver(self, forKeyPath:"videoBounds" , options: NSKeyValueObservingOptions.New, context: nil)]

}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{
    print("playerViewController.view.frame = \(playerViewController.view.frame)")

    if keyPath == "videoBounds"
    {
        let rect = change!["new"]! as! NSValue

        if let newrect = rect.CGRectValue() as CGRect?
        {
            if newrect.width > 0 || newrect.height > 0
            {
                if avWidth > 0 || avHeight > 0
                {
                    if newrect.width > avWidth || newrect.height > avHeight
                    {
                        print("Full Screen")
                    }
                    else if newrect.width < avWidth || newrect.height < avHeight
                    {
                        print("Normal screen")
                    }
                }
                avWidth = newrect.width
                avHeight = newrect.height
            }
        }
    }
}

However, it never seems to reach the code print("Full Screen"). It's hitting print("Normal Screen") regardless of whether the player is in normal or full screen mode.

Thanks!

like image 581
Pangu Avatar asked May 09 '16 02:05

Pangu


2 Answers

Starting from iOS 12 we can use these AVPlayerViewControllerDelegate delegate methods :

func playerViewController(AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
func playerViewController(AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
like image 105
Lawliet Avatar answered Oct 12 '22 23:10

Lawliet


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")
                }
            }
        }
    }
}
like image 4
Pangu Avatar answered Oct 13 '22 01:10

Pangu