我有一个支持所有界面方向的iPhone应用程序(iOS6).但是,当MPMoviePlayerController播放视频全屏时,应仅支持横向.
我在Stack Overflow上找到了以下解决方案,它的工作原理.
[[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; }
然而,一个令人讨厌的问题仍然存在,即如果视频以纵向方向退出全屏幕(在强制风景中播放之后),底层视图不会回转.我必须手动将设备旋转到横向并返回到纵向以触发方向的更新.有没有办法我可以以编程方式触发这种更新?
以下屏幕截图应该说明我的意思:
注意:由于各种原因,使用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,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height); }); }
这个代码在iOS6和iOS7中测试工作正常.谢谢 :)
请让我知道如果有任何问题…..