繁体   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