Como detectar o modo de tela cheia usando o AVPlayerViewController no Swift?
Estou tentando detectar quando oAVPlayerViewController
está no modo de tela cheia, mas estou tendo dificuldades para conseguir isso. Gostaria de saber quando o usuário seleciona o botão de expansão para entrar em tela cheia, como mostrado aqui:
Eu adicionei o observador apropriado por estas sugestões:
Detectar vídeo em tela cheia no modo retrato ou paisagemComo detectar o modo de tela cheia do AVPlayerViewControllerO código apropriado:
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
}
}
}
}
No entanto, nunca parece alcançar o códigoprint("Full Screen")
. Está batendoprint("Normal Screen")
independentemente de o player estar no modo normal ou em tela cheia.
Obrigado!