MPMoviePlayerController全屏旋转,而父视图控制器仅支持纵向

这个问题只是我的问题的一部分。 我正在为我现有的应用程序实现iOS6旋转和定向支持。

所以我有一个ViewController,它包含一个embedded在ViewController视图中的MPMoviePlayerController(我的应用程序需要它)。 用户可以播放video并在embedded式视图中查看,或使用默认播放器控件点击全屏button,播放器进入全屏模式。

现在我已经限制视图控制器只支持使用iOS6提供的新的旋转API的纵向方向。

// New Autorotation support. - (BOOL)shouldAutorotate; { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

这工作得很好。 ViewController只支持肖像和用户在embedded式视图中播放电影。

现在问题来了,当用户进入全屏模式。 在全屏模式下,当我旋转模拟器/设备时,电影保持旋转。 当我旋转设备而在全屏幕模式下使用shouldAutorotatesupportedInterfaceOrientations断点播放电影时,它仍然在这两个方法分别返回NOUIInterfaceOrientationMaskPortrait ,但仍然电影正在旋转…

为什么发生这种情况? ….这是我的问题的一部分…第二部分是我想要电影进入横向模式,当用户转到全屏模式。 我希望电影播放器​​locking在横向模式,直到用户按下“完成”button。

请帮忙 ….

你可以在AppDelegate下面的函数中尝试:

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { } 

你可以在这里为两种模式制定条件。

如媒体播放器在全屏幕中

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

否则return UIInterfaceOrientationMaskPortrait;

我没有尝试过,但我认为,它应该在你的情况下工作。

谢谢

为了清楚起见,这里是完整的代码(它全部进入你的应用程序委托):

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; } - (void)willEnterFullscreen:(NSNotification*)notification { NSLog(@"willEnterFullscreen"); isFullScreen = YES; } - (void)willExitFullscreen:(NSNotification*)notification { NSLog(@"willExitFullscreen"); isFullScreen = NO; } -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (isFullScreen) return UIInterfaceOrientationMaskLandscapeLeft; else return UIInterfaceOrientationMaskPortrait; } 

isFullScreen是要在AppDelegate.h中声明的BOOL

我会build议使用MPMoviePlayerViewController来代替。 inheritance它并实现supportedInterfaceOrientations方法并返回UIInterfaceOrientationMaskLandscape

您可能还必须实现shouldAutorotateToInterfaceOrientation:方法。

请参阅类参考: MPMoviePlayerViewController

编辑:你也可以看看这个post: iphone – 强制MPMoviePlayerController在横向模式下播放video

这消耗了我一段时间,我有这么多不同的可怕的错误,但最终我不是通过MPMoviePlayerController而是MPMoviePlayerViewController 。 我只是旋转self.playerView这是一个属性,在呈现之前。 此外,我添加了NSNotification将导致回到主控制和video完成后,主ViewController。 以下是我如何执行它:

  [[NSNotificationCenter defaultCenter] removeObserver:self.playerView name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerView.moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerView.moviePlayer]; self.playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:docUrl]; self.playerView.view.frame = CGRectMake(10, 10, self.frame.size.width-20, 180); [self.playerView.moviePlayer prepareToPlay]; if(IS_IPHONE_6P) { [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)]; [self.playerView.view setCenter:CGPointMake(212, 368)]; } else if(IS_IPHONE_6) { [self.playerView.view setBounds:CGRectMake(0, 0, 375, 667)]; [self.playerView.view setCenter:CGPointMake(187, 333)]; } else if (IS_IPHONE_5) { [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)]; [self.playerView.view setCenter:CGPointMake(160, 284)]; } else { [self.playerView.view setBounds:CGRectMake(0, 0, 480, 320)]; [self.playerView.view setCenter:CGPointMake(160, 240)]; } [self.playerView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; self.playerView.modalPresentationStyle = UIModalPresentationFormSheet; self.playerView.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentViewController:self.playerView animated:YES completion:nil]; 

而callbackmovieFinishedCallback :如下,

 - (void)movieFinishedCallback:(NSNotification*)aNotification { // Obtain the reason why the movie playback finished NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; // Dismiss the view controller ONLY when the reason is not "playback ended" if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) { MPMoviePlayerController *moviePlayer = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; NSLog(@"Video Closed"); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^ { [self dismissViewControllerAnimated:NO completion:nil]; self.playerView = nil; }); } } 

这对我有效。 希望能帮助到你。

在您的项目中,select名称项目和右窗口select信息选项卡。 在自定义的ios目标属性中添加键并select键:“初始界面方向”设置值:纵向(底部主页button)

  • 重build你的项目 – >确定

对于iOS 6,你可以使用这个答案 。

但是如果你支持<iOS 6需要一个不同的方法。

您必须创build自定义导航控制器,并为其添加使用根控制器和旋转方法创build的方法。

它看起来像: m文件和h文件 。

并且在你的AppDelegate中必须调用init的方法:

在h文件中:

 #import "IORNavigationController.h" 

 @property (nonatomic, retain) IORNavigationController* navigationController; 

在m文件中:

 self.navigationController = [[[MyNavigationController alloc] initWithRootViewController:start] autorelease]; 

用这个

 moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI/2); 

它与ios 7一起工作

只需将此代码添加到您的视图控制器

 -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }