2013-06-24 7 views
19

私のアプリでビデオをストリーミングしようとしています。私が見つけた方法は次の通りです:MPMoviePlayerViewController |ランドスケープモードを許可する

NSURL *theMovieURL = [NSURL URLWithString:self.data.trailer]; 
     if (theMovieURL) 
     { 
      self.movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:theMovieURL]; 
      [self presentMoviePlayerViewControllerAnimated:self.movieController]; 
      [self.movieController.moviePlayer play]; 
     } 

私はそれが最も一般的であるかどうかは分かりませんが、それはうまくいきます。

問題は、私は、風景モードを許可する方法をビデオでしか理解できないということです。 shouldAutorotateまたはshouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationのようなものを使用する必要がありますか?

FYI、アプリ全体はポートレートモードのみを許可します。

ありがとうございました。

答えて

34

shouldAutoRotateは、iOS 6では廃止されており、<に行く場合を除き、避けてください。

代わりに、supportedInterfaceOrientationspreferredInterfaceOrientationForPresentationのメソッドをオーバーライドする必要があります。この場合

、あなたはそのようなアプリデリゲートにメソッドをオーバーライドすることができ、メディアプレーヤーをサブクラス化しない場合:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) 
    { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
+12

presentViewControllerが却下されているかどうかをチェックします(isBeingDismissedプロパティ)。そうでない場合は、プレゼンテーションビューコントローラがlandscapemodeに表示されます – peko

17

正しい方法を言う@peko。ユーザーが全画面ビデオを終了すると、このメソッドはMPMoviePlayerViewControllerクラスで再度呼び出します。あなたはそれが解雇されているかどうかをチェックする必要があります。そうしないと、ユーザーはビデオを終了し、メインウィンドウはランドスケープモードに留まり、さらにMPInlineVideoFullscreenViewControllerクラスを忘れてしまいます。埋め込みプレーヤー(フルスクリーンではない)を使用すると、そのクラス名で呼び出されます。

私はそれを好きでした。わたしにはできる。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx 
{ 
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] || 
    [[self.window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")]) 
    { 
     if ([self.window.rootViewController presentedViewController].isBeingDismissed) 
     { 
      return UIInterfaceOrientationMaskPortrait; 
     } 
     else 
     { 
      return UIInterfaceOrientationMaskAllButUpsideDown; 
     } 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
0

あなたの代わりにAVPlayerViewControllerを使用し、中にあまりにもそのクラスを確認する必要がありますので、MPMoviePlayerViewControllerが廃止されましたが、あなたはこの

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    UIViewController* playerController = self.window.rootViewController.presentedViewController; 
    while (playerController && ![playerController isKindOfClass:[MPMoviePlayerViewController class]]) { 
     playerController = playerController.presentedViewController; 
    } 
    if (playerController && !playerController.isBeingDismissed) { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } else { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

ような何かをする必要がありますので、ムービープレイヤーは、最初に提示ビューコントローラではないかもしれませんこのループ。

0

これまでの最良の方法は、このAppDelegate機能を実装し、rootViewControllerは、あなたが[.portrait, .landscapeLeft, .landscapeRight]、他.portraitを返すタイプAVPlayerViewControllerまたはMPMoviePlayerViewControllerの子を持っているかどうかを確認することです。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let _ = UIApplication.shared.keyWindow?.rootViewController?.childViewControllers.first as? AVPlayerViewController { 
     return [.portrait, .landscapeLeft, .landscapeRight] 
    } 
    return .portrait 
} 

あなたは、これはとても気をつけて動作しませんAppDelegateで宣言されているウィンドウにそのチェックを行うにしようとした場合、Appleはその別のUIWindowでこのViewControllerをを提示しているためあなたはUIApplicationkeyWindowに確認する必要があります。

関連する問題