强制整个屏幕上的横向方向MPMoviePlayerController在退出全屏时阻止正确的旋转

我有一个支持所有接口方向的iPhone应用程序(iOS6 +)。 但是,当MPMoviePlayerController正在全屏播放video时,只应支持横向。

我发现堆栈溢出下面的解决scheme,它的工作原理。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.landscapeOnlyOrientation) { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; } return UIInterfaceOrientationMaskAll; } - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification { self.landscapeOnlyOrientation = YES; } - (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notification { self.landscapeOnlyOrientation = NO; } 

然而,一个恼人的问题仍然存在,即如果video以纵向方式退出全屏(在强制横向播放之后),则底层视图不会旋转回来。 我必须手动将设备旋转到横向,然后回到纵向触发更新方向。 有什么办法可以触发这种更新程序?

下面的一组截图应该说明我的意思:

在这里输入图像说明在这里输入图像说明在这里输入图像说明

注意:由于各种原因,使用MPMoviePlayerViewController是不可能的。

大家好,我有同样的问题,我解决了它 –

这是我的完整代码….

您需要先在appdelegate中更改:

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here { return UIInterfaceOrientationMaskAll; } return UIInterfaceOrientationMaskPortrait; } 

注册全屏控制通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 

然后在播放器控制器中添加一行代码:

 - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification { dispatch_async(dispatch_get_main_queue(), ^ { self.allowRotation = YES; }); } - (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification { self.allowRotation = NO; [self.moviePlayerController setControlStyle:MPMovieControlStyleNone]; dispatch_async(dispatch_get_main_queue(), ^ { //Managing GUI in pause condition if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) { [self.moviePlayerController pause]; if (self.playButton.selected) self.playButton.selected = NO; } self.view.transform = CGAffineTransformMakeRotation(0); [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); }); } 

此代码已在iOS6和iOS7中正常工作。 谢谢 :)

请让我知道是否有任何问题…..

您需要inheritance并提供横向作为支持的界面方向。

 @interface MyMovieViewController : MPMoviePlayerViewController @end @implementation MyMovieViewController - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); } @end 

您可以尝试“强制”方向刷新,让系统为您处理正确的方向:

 - (void)forceOrientationRefresh { // Force orientation refresh [self presentModalViewController:[UIViewController new] animated:NO]; [self dismissModalViewControllerAnimated:NO]; } 

这是黑客,但它的工作。

这可能听起来很疯狂,但是,您可以尝试在打开video视图控制器之前先保存本地最后一个方向,然后使用application:supportedInterfaceOrientationsForWindow:返回保存的方向,并强制视图控制器停留在其上而不旋转。

你可以像这样以编程方式改变你的方向

 -(void)viewDidAppear:(BOOL)animated { if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft ); } } } 

并且不要忘记添加#import <objc/message.h>

我认为你可以注册你的viewcontroller设备的方向和强制调用viewcontroller的方向的方法。

您使用supportedIterfaceOrientationsForWindow,然后查找: MPInlineVideoFullscreenViewController 。 find正确的视图控制器是有点棘​​手,但它的工作。

这里是示例代码:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ if ([NSStringFromClass([self.window.rootViewController.presentedViewController.presentedViewController class]) isEqualToString:@"MPInlineVideoFullscreenViewController"]){ return UIInterfaceOrientationMaskAllButUpsideDown; } return UIInterfaceOrientationMaskLandscape; } 

你需要添加这个代码为iOS7它的作品完美和简单

 -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } .... creating a player MPMoviePlayerViewController *mp =[[MPMoviePlayerViewController alloc] initWithContentURL:url]; ...make settings and present it [self presentMoviePlayerViewControllerAnimated:mp];