簡體   English   中英

如何在 Swift 中使用 AVPlayerViewController 檢測全屏模式?

[英]How to detect fullscreen mode using AVPlayerViewController in Swift?

我試圖檢測AVPlayerViewController何時處於全屏模式,但我很難實現這一點。 我想知道用戶何時選擇展開按鈕進入全屏,如下所示:

在此處輸入圖片說明

我已經根據這些建議添加了適當的觀察者:

  1. 檢測縱向或橫向全屏播放的視頻
  2. 如何檢測 AVPlayerViewController 的全屏模式

相應的代碼:

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

但是,它似乎永遠不會到達代碼print("Full Screen") 無論播放器處於正常模式還是全屏模式,它都會點擊print("Normal Screen")

謝謝!

從 iOS 12 開始,我們可以使用這些AVPlayerViewControllerDelegate委托方法:

func playerViewController(AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
func playerViewController(AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)

為 Swift 3 更新

playerViewController對象添加一個觀察者:

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

您的代碼幫助我處理全屏和返回之間的切換。

但是對於識別部分,我只是根據我的要求稍微更改了它。

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

希望這可以幫助。

在 iOS11 中,如果 AVPlayer 全屏顯示,您屏幕的安全區域將降至 0。 雖然它可能是一個未記錄的功能(因此有潛在的錯誤)。 我很難找到有關它的更多信息。

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

這是@Pangu 答案的略微優化的 Swift 4.2 版本。 它只檢測變化,否則在與視頻交互(如快進)時也會調用觀察者。 我還用AVPlayerViewController.videoBounds鍵路徑替換了“videoBounds”以避免字符串並使用窗口邊界來確定它是否是全屏的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM