2017-03-22 13 views
1

デバイスがポートレートのときにビデオを再生するコンテナビューがあります。 デバイスが回転しているときにビデオが自動的にフルスクリーンで再生されるようにしたい。 私は、デバイスを回転させたときに検出し、このコードを試してみたが、それは、フルスクリーンswift 3 - 風景に回転させるとフルスクリーンでビデオを再生する

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プレーヤーであるにコンテナを変更しません。 ありがとうございました

+0

「layoutConstraints」によってあなたの問題を助けることができますか? 'videoView'を' containerView'にバインドするだけです。 – RrrangeTop

+0

既にバインドされています... –

+0

私は、回転させたときにAvplayerControllerを画面サイズに設定して解決策を見つけました。今私はちょうどnavBarを却下する方法を見つける必要があります –

答えて

1

O.k AVPlayerLayerにビデオを追加し、デバイスが横長のときはそのサイズをビューに設定することで、ビデオフルスクリーンを再生することができました。 UIDevice.cuurent.orientation.islandscapeは== falseを私はサブレイヤを削除し、バックvideoContainer境界にビューを設定し

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

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


    } 

誰にでも役立つことを願っています。

0

この方法でコントロールが表示されるため、この問題を解決する別のより良い方法が見つかりました。主なアイデアは、AVPlayerViewControllerオブジェクトを新しいサブビューに移動し、そのフレームをフルスクリーンに設定することです。ここに私のコードです。

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

希望します。がんばろう!

関連する問題