在没有NSNotificationCenter的iOS 8中正确强制或允许Youtubeembeddedvideo的横向模式

我遇到的问题是,在iOS 8中,youtube播放器将以全屏方式进入或退出全屏模式,这是因为这些通知已被移除UIMoviePlayerControllerDidEnterFullscreenNotificationUIMoviePlayerControllerWillExitFullscreenNotification

由于我的应用程序项目设置为仅在肖像模式下,video在播放时不会旋转到横向模式,而在您的设备上观看video时,实际上并不太方便用户使用。

通常用户想要在全屏模式下以纵向模式或横向模式观看video。

这是我做iOS 7的方式 ,这是完美的,但不是在iOS 8。

首先,我将在我的AppDelegate.m使用布尔属性在我的AppDelegate.h设置这个函数,我称之为videoIsInFullscreen和函数,

 // this in the AppDelegate.h @property (nonatomic) BOOL videoIsInFullscreen; // This in my AppDelegate.m to allow landscape mode when the boolean property is set to yes/true. - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ if(self.videoIsInFullscreen == YES) { return UIInterfaceOrientationMaskAllButUpsideDown; } else { return UIInterfaceOrientationMaskPortrait; } } 

然后,在我的ViewController.m首先,我会#import "AppDelegate.h"后,我会添加一些通知在我的viewDidLoad方法..

 -(void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEnded) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; } 

当然不要忘了删除它们..

 -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; } 

然后,我有我的function,当这些通知火灾时,将得到调用…这里是我允许横向模式,然后将其设置回肖像。 这是我的应用程序的情况,因为它只设置为肖像支持,但我不希望这个YouTubevideo。

 // first we set our property in the our AppDelegate to YES to allow landscape mode - (void)playerStarted { ((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = YES; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; } // Then I will set the property to NO and force the orientation to rotate to portrait. - (void)playerEnded { ((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = NO; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; } 

但是,这些不是iOS 8的情况..这些通知不再适用于iOS 8,所以我发现类似的使用这些通知,但我不是很高兴,因为他们不是100%准确的video播放器。 UIWindowDidBecomeVisibleNotificationUIWindowDidBecomeHiddenNotification那么,我怎样才能正确地做到这一点,或者至less可以适用于我的YouTubeembedded式video,并允许横向模式在iOS 8 …?

所以,经过一些研究,并深入探讨这个问题..我来到一个使用UIWebView委托的解决scheme,再加上我不得不解决另一个问题,我的function- (void)playerEnded它不能正常工作在新的iPhone 6设备

这是我怎么做的。首先,在我的webViewDidFinishLoad方法,我已经添加到我的webview javascript评估,以检查此video播放器何时进入全屏模式..

 - (void)webViewDidFinishLoad:(UIWebView*)webView { // adding listener to webView [_webView stringByEvaluatingJavaScriptFromString:@" for (var i = 0, videos = document.getElementsByTagName('video'); i < videos.length; i++) {" @" videos[i].addEventListener('webkitbeginfullscreen', function(){ " @" window.location = 'videohandler://begin-fullscreen';" @" }, false);" @"" @" videos[i].addEventListener('webkitendfullscreen', function(){ " @" window.location = 'videohandler://end-fullscreen';" @" }, false);" @" }" ]; } 

然后,在我的- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType方法,我检查当我的请求url匹配的YouTube播放器的状态,像这样..这将发射我们的function,允许横向模式或强制回到肖像模式..或者任何其他types的工作,你可能想要做的..

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // allows youtube player in landscape mode if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=3"]) { [self playerStarted]; return NO; } if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=2"]) { [self playerEnded]; return NO; } } 

最后,我需要调整我的playerEndedfunction强制为iPhone 6设备的背部肖像模式..

 - (void)playerEnded { [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; ((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = NO; [self supportedInterfaceOrientations]; [self shouldAutorotate:UIInterfaceOrientationPortrait]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; } 

差不多,错过了我还加了这两个其他的function..

 - (NSInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

所以,最后我能够抓住实际玩家的状态,并在适当的时候启动我的function做一些工作或任何我想要的东西,在我的情况下改变方向..

我希望这可以帮助别人..

我正在快速的工作,我的播放器在纵向和横向都运行电影。 首先我检查了三种模式:肖像,风景,风景。 其次,我在所有的viewController中都写了这个函数:

  isFullScreen = false override func shouldAutorotate() -> Bool { if isFullScreen == true { return true }else{ return false } } 

第三,我在这个函数中改变了isFullScreen的值:

 func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) { switch (state) { case YTPlayerState.Playing: println("started to play") isFullScreen == true shouldAutorotate() case YTPlayerState.Paused: println("paused") default: println("non of sttate") break } } 

video在纵向和横向模式下运行! 有趣的是,当我暂停video或从全屏移动时,我不再设置isFullScreen为false。 但它不旋转! 有人可以解释吗?