2016-05-09 84 views
4

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

enter image description here

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

  1. Detect Video playing full screen in Portrait or landscape
  2. 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")

謝謝!

回答

2

更新了斯威夫特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") 
       } 
      } 
     } 
    } 
} 
0

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

但是對於識別部分,我只是稍微改變了它以符合我的要求。

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

希望這會有所幫助。

+0

但全屏幕大小不匹配UIScreen.mainScreen()。bounds.size –

0

在iOS11屏幕的安全區將下降到0在AVPlayer去全屏的事件。雖然它可能是一個無證的功能(因此可能的bug)。我很難找到關於它的更多信息。

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

相關問題