【发布时间】:2018-06-07 19:02:59
【问题描述】:
我试图检测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")。
谢谢!
【问题讨论】:
-
您好,我也遇到了同样的问题,您已经解决了吗?谢谢
标签: ios swift avplayer avplayerviewcontroller