MPMoviePlayerViewController | 允许横向模式
我正在尝试在我的应用程序中传输video。 我发现的方法是:
NSURL *theMovieURL = [NSURL URLWithString:self.data.trailer]; if (theMovieURL) { self.movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:theMovieURL]; [self presentMoviePlayerViewControllerAnimated:self.movieController]; [self.movieController.moviePlayer play]; }
我不确定这是否是最传统的,但是它是有效的。
问题是,我不知道如何只允许video的横向模式。 我应该使用像shouldAutorotate
或shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
,以及如何?
仅供参考,整个应用程序只允许肖像模式。
谢谢你的帮助。
shouldAutoRotate
从iOS 6开始已经被弃用了,应该避免这种情况,除非你要做的是<6。
相反,你会想重写supportedInterfaceOrientations
和preferredInterfaceOrientationForPresentation
方法。
在这种情况下,如果您不想子类化媒体播放器,则可以在应用程序委托中重写一个方法,如下所示:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) { return UIInterfaceOrientationMaskAllButUpsideDown; } else { return UIInterfaceOrientationMaskPortrait; } }
@佩科说正确的方法。 当用户从全屏video退出时,此方法再次用MPMoviePlayerViewController
类调用。 你应该检查它是否被解雇,如果你不这样做,当用户退出video时,主窗口将保持横向模式,并且你忘了MPInlineVideoFullscreenViewController
类。 当你使用embedded式播放器(不是全屏)时,它被称为该类名称。
我是这样做的。 这个对我有用。
- (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; } }
电影播放器可能不是第一个提供的视图控制器,所以你应该这样做
- (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
而不是在这个循环中检查它的类。
目前为止,最好的方法是实现这个AppDelegate函数,并检查rootViewController
是否有一个types为AVPlayerViewController
或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 }
你应该检查UIApplication
的keyWindow
,因为苹果在另一个UIWindow
提供了这个viewController,所以如果你试图在AppDelegate
声明的窗口检查,这将不会工作,所以要小心。