全屏播放YouTube视频,仅允许针对iOS7和iOS8的视频进行旋转

时间:2014-05-23 13:56:23

标签: video ios7 youtube ios8 autorotate

我试图在UIWebView上播放YouTube视频,如下所示:

// Create the URL
_videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];

// Create the request with the URL
NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];

// Load the request into the Web View
[_webView loadRequest:requestObj];

youtube页面显示,当我对视频开始播放时,但它没有旋转。

我花了一周时间寻找不同的解决方案,通过实施" shouldAutorotate"和#34; supportedInterfaceOrientations",没有成功!

我尝试的最后一件事是在视频以全屏模式播放时添加一个监听器,在AppDelegate.m中我添加了" didFinishLaunchingWithOptions"代码如下:

[[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 {
self.allowRotation = YES; }

- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO; }

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.forceLandscapeRight) {
    return UIInterfaceOrientationMaskLandscapeRight;
}
if (self.allowRotation) {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

return UIInterfaceOrientationMaskPortrait; }

问题在于,#34; moviePlayerWillEnterFullscreenNotification"或者" moviePlayerWillExitFullscreenNotification"被称为。

请帮忙!

3 个答案:

答案 0 :(得分:2)

找到答案:

我必须使用

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];

而不是

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

在我的ViewControler中实现方法

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

有关详情:iOS 6.0+ autorotation for youtube embedded video

希望这会有所帮助:)

答案 1 :(得分:2)

这是我在Swift中解决问题的方法。它基于上面的答案,但更简单:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
  // if it's our window, portrait. Other windows are (probably) the fullscreen video player ;)
  if self.window == window {
    return .Portrait
  } else {
    return [.Portrait, .Landscape]
  }
}

答案 2 :(得分:1)

我刚做了一些研究,我想出了支持iOS7和iOS8的解决方案。 要仅在播放YouTube视频时允许旋转应用程序,请执行以下步骤:

  1. AppDelegate.m
  2. 导入#import <MediaPlayer/MediaPlayer.h> 并实现函数&#34; supportedInterfaceOrientationsForWindow&#34;如下:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
    if ([[window.rootViewController presentedViewController]
         isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
    
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }else {
    
        if ([[window.rootViewController presentedViewController]
             isKindOfClass:[UINavigationController class]]) {
    
            // look for it inside UINavigationController
            UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController];
    
            // is at the top?
            if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
                return UIInterfaceOrientationMaskAllButUpsideDown;
    
                // or it's presented from the top?
            } else if ([[nc.topViewController presentedViewController]
                        isKindOfClass:[MPMoviePlayerViewController class]]) {
                return UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }
    
    return UIInterfaceOrientationMaskPortrait;
    }
    
    1. ViewController.m
    2. 在你的&#34; ViewController.m&#34;上,将以下代码添加到&#34; ViewDidLoad&#34;,此代码允许您在视频以全屏模式播放时创建一个监听器:

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          // SetUp notifications when video gets played and stopped
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
      
          // Init The Video WebView
          [self initVideoWebView];
      }
      

      此功能允许您使用YouTube网址启动WebView:

      - (void) initVideoWebView {
      
          // Create the URL
          _videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];
      
          // Create the request with the URL
          NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];
      
          // Load the request into the Web View
          [_webView loadRequest:requestObj];
      }
      

      最后要做的是在ViewControler上实现以下功能:

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

      如果这有帮助,上升率:)