我試圖檢測何時AVPlayerViewController
處於全屏模式,但我很難實現這一點。我想,當用戶選擇的展開按鈕進入全屏知道如下所示:如何在Swift中使用AVPlayerViewController檢測全屏模式?
我已經添加了每這些建議適當的觀察:
- Detect Video playing full screen in Portrait or landscape
- How to detect fullscreen mode of 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")
。
謝謝!
但全屏幕大小不匹配UIScreen.mainScreen()。bounds.size –