2017-08-30 68 views
0

所以這裏是我的使用案例 - 我們正在使用avplayer加載視頻並播放它,用戶可以點擊默認的全屏按鈕以全屏模式播放視頻。如果用戶已登錄並且未登錄,用戶可能會在兩種不同的條件下觀看視頻。iOS Swift不能關閉avplayer模式

如果用戶已登錄(由變量值確定),則他可以觀看完整的視頻,否則,播放應在播放指定的秒數後停止(取決於視頻中秒數的變化),並在播放器上出現橫幅,要求其登錄。

內聯播放視頻時,一切正常。但是,當視頻播放全屏時,即使我們使用didTapPause()停止播放,全屏窗口也不會被解除。我甚至嘗試使用self.playerController.dismiss(animated: true, completion: nil)解僱它,全屏模式並沒有被解僱。代碼片段是如下 -

private func setUpView() { 
    self.backgroundColor = .black 
    addVideoPlayerView() 
    configurateControls() 
} 

fileprivate func addVideoPlayerView() { 
    playerController.view.frame = self.bounds 
    playerController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    playerController.showsPlaybackControls = true 
    playerController.addObserver(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil) 
    self.insertSubview(playerController.view, at: 0) 
} 

我不知道這是否是正確的方法 -

playerController.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in 
     if self.playerController.player?.currentItem?.status == .readyToPlay { 
      self.videoCurrentTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem!.currentTime())!); 
      self.videoTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem?.duration)!); 



      if self.moveToTime != nil{ 
       let timeWithSecond = CMTimeMakeWithSeconds(self.videoTimeDuration! * self.moveToTime!/100, Int32(kCMTimeMaxTimescale)) 

       self.playerController.player?.seek(to: timeWithSecond, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) 
       self.moveToTime = nil 
      } 

      guard let videoD = self.videoData else { return } 

      let timeTToPlay: Double = Double(videoD.freeDuration) 

      if videoD.isFree { 
       if videoD.registrationNeeded && !CurrentLoginUser.shared.isLogin{ 
        if self.videoCurrentTimeDuration! > timeTToPlay { 
         self.didTapPause() 

         self.playerController.dismiss(animated: true, completion: nil) 


         self.loginNeedView = UINib.get(withNib: self) 
         self.loginNeedView?.frame = self.bounds 

         self.loginNeedView?.autoresizingMask = [ 
          UIViewAutoresizing.flexibleWidth, 
          UIViewAutoresizing.flexibleHeight 
         ] 
         self.addSubview(self.loginNeedView!) 
         AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait) 
        } 
       } 
       else{ 
        self.loginNeedView?.removeFromSuperview() 
        AppUtility.lockOrientation(UIInterfaceOrientationMask.all) 
       } 
      } 

玩家控制器通過調用setupView功能,如下所示添加到視圖做到這一點,有什麼想法?

編輯:

接着按anbu.karthik的建議我試圖通過定位頂視圖控制器作爲強行取出全屏視圖:

func currentTopViewController() -> UIViewController { 
var topVC: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController 
while ((topVC?.presentedViewController) != nil) { 
topVC = topVC?.presentedViewController 
} 
return topVC! 
} 

,然後,用它作爲如下 -

let currentTopVC: UIViewController? = self.currentTopViewController() 

         if (currentTopVC?.description.contains("AVFullScreenViewController"))! { 
          print("found it") 

          currentTopVC?.dismiss(animated: true) { _ in } 
         } 

和它的作品,但與下面的異常崩潰的應用程序 -

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<AVEmbeddedPlaybackControlsViewController: 0x7fdcc2264400> should have parent view controller:<AVPlayerViewController: 0x7fdcc222a800> but actual parent is:<AVFullScreenViewController: 0x7fdcce2f3c50>' 
+0

您沒有刪除'addPeriodicTimeObserver' –

回答

0

據我所知,不可能直接播放全屏。看起來,AVPlayerViewController幾乎是一樣的,並沒有提供很多定製UI或行爲的方式。如果您想直接播放全屏,則需要以全屏模式呈現包含AVPlayerViewController的控制器,或者自行更改框架以使其全屏顯示。但是沒有API在AVPlayerViewController上以編程方式控制全屏...