【问题标题】:swift 3 - play video in fullscreen when rotated to landscapeswift 3 - 旋转到横向时全屏播放视频
【发布时间】:2017-03-22 09:18:13
【问题描述】:

我有一个容器视图,可以在设备为纵向时在其中播放视频。 我希望视频在设备旋转时自动全屏播放。 我试过这段代码来检测设备何时旋转,但它不会将容器更改为全屏

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {

        videoContainerView.frame = self.view.frame
       // self.videoContainerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) // this didn't work to....
        print("Device is landscape")
    }
    else{
        print("Device is portrait")
    }

}

视频在 avplayer 播放器上。 谢谢

【问题讨论】:

  • May by layoutConstraints 可以帮助您解决问题吗?只需将您的 videoView 绑定到 containerView
  • 他们已经绑定在一起了……
  • 我找到了一个解决方案,方法是将 AvplayerController 设置为旋转时的屏幕大小。现在我只需要找到一种方法来关闭导航栏

标签: ios video swift3 autorotate


【解决方案1】:

我找到了另一种更好的方法来解决这个问题,因为这样控件是可见的。主要思想是将 AVPlayerViewController 对象移动到一个新的 subView 并将它的框架设置为全屏。这是我的代码..

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {


                    DispatchQueue.main.async {
                        self.playerController.player = self.player
                        self.addChildViewController(self.playerController)
                        self.view.addSubview(self.playerController.view)
                        self.playerController.view.frame = self.view.frame

        }
        print("Device is landscape")
    }

当设备旋转回纵向时,将此子视图添加到前一帧并再次设置它的大小。这是我的代码。

else{
        print("Device is portrait")
        videoContainerView.addSubview(playerController.view)
        playerController.view.frame = videoContainerView.bounds
        controllsContainerView.frame = videoContainerView.bounds
    }
}

希望对您有所帮助。祝你好运!

【讨论】:

    【解决方案2】:

    好的。所以我设法通过将视频添加到 AVPlayerLayer 并在设备为横向时将其大小设置为视图来全屏播放视频。

    override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    
        if (UIDevice.current.orientation.isLandscape) {
    
                        DispatchQueue.main.async {
                           self.view.didAddSubview(self.controllsContainerView)
                           self.layer = AVPlayerLayer(player: self.player!)
                            self.layer?.frame = self.view.frame
                            self.layer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                            self.view.layer.addSublayer(self.layer!)
    
            }
            print("Device is landscape")
        }
    

    当 UIDevice.cuurent.orientation.islandscape == false 我删除 subLayer 并将视图设置回 videoContainer 边界。

    else{
            print("Device is portrait")
            movie.view.frame = videoContainerView.bounds
            controllsContainerView.frame = videoContainerView.bounds
            self.layer?.removeFromSuperlayer()
    
    
        }
    

    希望它对任何人都有帮助。

    【讨论】:

      【解决方案3】:

      这对我有用,因为我使用的是 AVQueuePlayer:

      override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
          super.willTransition(to: newCollection, with: coordinator)
          DispatchQueue.main.async {
              if (UIDevice.current.orientation.isLandscape) {
                  self.landScapeAVController           = AVPlayerViewController()
                  self.addChild(self.landScapeAVController!)
                  self.landScapeAVController!.player   = self.queuePlayer
                  self.landScapeAVController!.view.tag = 999
                  self.view.addSubview(self.landScapeAVController!.view)
              } else {
                  let topView                          = self.view.viewWithTag(999)
                  topView?.removeFromSuperview()
                  self.landScapeAVController?.removeFromParent()
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-21
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多