2013-06-24 78 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,以及如何?

仅供参考,整个应用程序只允许肖像模式。

感谢您的帮助。

回答

34

shouldAutoRotate从iOS 6开始已弃用,应该避免使用,除非您打算使用< 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属性),否则呈现viewcontroller将显示在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

的电影播放器​​可能不是第一次提出视图控制器,所以你应该做这样的事情

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

虽然MPMoviePlayerViewController已被弃用,所以你应该使用AVPlayerViewController,而不是太检查其类这个循环。

0

到目前为止,最好的办法就是实现这个功能的AppDelegate和检查rootViewControllerAVPlayerViewController型或MPMoviePlayerViewController的孩子,那么你回到[.portrait, .landscapeLeft, .landscapeRight]和其他.portrait

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

你应该对UIApplicationkeyWindow检查,因为苹果提出这个的viewController在另一UIWindow所以如果你尝试这样做,声明在AppDelegate这是不行的,所以要小心窗口该检查。