How to detect fullscreen mode using AVPlayerViewController in Swift?

前端 未结 5 563
花落未央
花落未央 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 18:41

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

    func playerViewController(AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
    func playerViewController(AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
    
    0 讨论(0)
  • 2020-12-10 18:49

    In iOS11 your screen's safe area will drop to 0 in the event of an AVPlayer going fullscreen. Though it might be an undocumented feature (and therefor potential bug). I am having a hard time finding more info on it.

    [UIApplication sharedApplication].keyWindow.safeAreaLayoutGuide.layoutFrame.size.height == 0?

    0 讨论(0)
  • 2020-12-10 19:02

    Your code helped me handle switching between full screen and back.

    But for the identification part, I just changed it slightly to my requirement.

    let rect = change!["new"] as! NSValue
    
    if let playerRect: CGRect = rect.CGRectValue() as CGRect {         
       if playerRect.size == UIScreen.mainScreen().bounds.size {
          print("Video in full screen")
       } else {
          print("Video not in full screen")
       }
    }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 19:04

    This is a slightly optimized Swift 4.2 version of @Pangu's answer. It only detects the change, otherwise the observer is called also when interacting with the video like fast forwarding. I also replaced the "videoBounds" with the AVPlayerViewController.videoBounds keypath to avoid the string and use the window bounds to determine if it's fullscreen or not.

    avPlayerViewController.addObserver(self, forKeyPath: #keyPath(AVPlayerViewController.videoBounds), options: [.old, .new], context: nil)
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == #keyPath(AVPlayerViewController.videoBounds) {
            // detect only changes
            if let oldValue = change?[.oldKey] as? CGRect, oldValue != CGRect.zero, let newValue = change?[.newKey] as? CGRect {
                // no need to track the initial bounds change, and only changes
                if !oldValue.equalTo(CGRect.zero), !oldValue.equalTo(newValue) {
                    if newValue.size.height < UIScreen.main.bounds.height {
                       print("normal screen")
                    } else {
                       print("fullscreen")
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 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")
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题