Youtube视频无法在iOS 8中以横向模式播放

时间:2014-09-22 06:09:38

标签: ios iphone video youtube ios8

我的应用程序包括在横向和纵向模式下播放视频的功能。视频可以是youtube也一切正常,直到iOS 7,但现在youtube视频在iOS 8上无法在横向模式下工作。

我的代码:

- (NSUInteger)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(UIWindow *)window {


  if ([[window.rootViewController presentedViewController]
     isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")])        {

      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;
}

一切正常,直到iOS 7,但停止在iOS 8上工作。 任何帮助表示赞赏

6 个答案:

答案 0 :(得分:14)

有时候回答你自己的问题很傻,但是对于那些面临同样问题的人来说这很有帮助。

在iOS 8中,我们需要检查AVFullScreenViewController,而不是检查MPInlineVideoFullscreenViewController。以下是所有iOS版本的完整方法,即iOS 8及更低版本。

 - (NSUInteger)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(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;
}

更新 也可以在iOS 9中使用

答案 1 :(得分:5)

我的应用程序中有类似的代码,在iOS8中也有破解。

我只想发布此修补程序的版本以防万一。

主要区别在于,我只是检查最顶层的控制器。

我认为这比嵌套条件试图弄清楚什么样的vc呈现另一个vc更有意义。

无论如何,我已经在我的应用代表中得到了这个,并且它在8中工作得很好。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    id presentedViewController = [self topMostController];

    if ( [self vcIsVideoPlayer:presentedViewController] ) {
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

- (UIViewController*) topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

- (BOOL) vcIsVideoPlayer:(UIViewController *)vc {
    NSString *className = vc ? NSStringFromClass([vc class]) : nil;
    return (
            [className isEqualToString:@"MPInlineVideoFullscreenViewController"] ||
            [className isEqualToString:@"MPMoviePlayerViewController"] ||
            [className isEqualToString:@"AVFullScreenViewController"]
            );
}

答案 2 :(得分:3)

<强>更新: 从景观视频返回控制器后,如果您注意到状态栏中断,则添加一件事就是在viewWillLayoutSubviews中将状态栏设置为不隐藏为false。

   override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .None)
}

对于那些在Swift中的人,还有一些补充说明。此方法(application:supportedInterfaceOrientationsForWindow)应该在您的AppDelegate类中,或者您设置为@UIApplicationMain的任何内容。要访问MPMoviePlayerViewController课程,您必须记得import MoviePlayer

其次,UIInterfaceOrientationMask值本身与代理的Swift版本不兼容,因此您需要访问rawValue并将生成的Uint转换为Int }。这是针对有需要的人的Swift解决方案。

    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
    var orientation =  UIInterfaceOrientationMask.Portrait

    if let presentedController = window.rootViewController?.presentedViewController {

        //check for the controllers
        if presentedController is MPMoviePlayerViewController ||
           presentedController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) ||
           presentedController.isKindOfClass( NSClassFromString("MPInlineVideoFullscreenViewController").self ) {
           orientation = .AllButUpsideDown
        }
        //otherwise, we may be inside a Nav.
        //safely get the nav controller otherwise ignore this block
        else if let navController = presentedController as? UINavigationController {

            if navController.topViewController is MPMoviePlayerViewController ||
               navController.topViewController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self )  ||
               navController.topViewController.isKindOfClass( NSClassFromString("MPInlineVideoFullscreenViewController").self ) {
               orientation = .AllButUpsideDown
            }
        }


    }

   return  Int(orientation.rawValue)
}

答案 3 :(得分:1)

这是在iOS7和iOS8上测试的Swift解决方案。您必须将此方法添加到AppDelegate类。

AppDelegate.swift

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

    var topController = UIApplication.sharedApplication().keyWindow?.rootViewController

    if (topController != nil) {
        while ((topController!.presentedViewController) != nil) {
            topController = topController!.presentedViewController;
        }

        if (topController != nil && (topController!.className == "AVFullScreenViewController" || topController!.className == "MPFullScreenTransitionViewController"))  {
            return Int(UIInterfaceOrientationMask.All.rawValue);
        }

    }

    return Int(UIInterfaceOrientationMask.Portrait.rawValue);
 }

答案 4 :(得分:0)

我无法在iOS 8 Golden Master的应用程序中检测到AVFullScreenViewController,但找到AVPlayerView就可以了。

的UIViewController + VideoAutorotate.h

#import <UIKit/UIKit.h>

@interface UIViewController (VideoAutorotate)

@end

的UIViewController + VideoAutorotate.m

#import "UIViewController+VideoAutorotate.h"

BOOL testAnyViewRecursively(UIView *view, BOOL (^test)(UIView *view)) {
    if (test(view)) {
        return YES;
    } else {
        for (UIView *subview in view.subviews) {
            if (testAnyViewRecursively(subview, test)) {
                return YES;
            }
        }
    }
    return NO;
}

@implementation UIViewController (VideoAutorotate)

-(BOOL)shouldAutorotate
{
    if (UI_PAD) {
        return YES;
    } else {
        // iOS 6: MPInlineVideoFullscreenViewController in iOS 6 doesn't seem to override this method to return YES.
        if ([NSStringFromClass([self class]) isEqual:@"MPInlineVideoFullscreenViewController"]) {
            return YES;
        }
        // iOS 8:
        return testAnyViewRecursively(self.view, ^BOOL(UIView *view) {
            return [NSStringFromClass([view class]) isEqual:@"AVPlayerView"];
        });
    }
}

答案 5 :(得分:0)

这是适用于iOS 10.1的Swift 3版本。我在这里修改了Anthony Persaud的答案。要查看// IB: Record Button Control @IBAction func recordVideo(_ sender: UIButton) { if (UIImagePickerController.isSourceTypeAvailable(.camera)) { self.selectedButton = sender self.imagePicker.sourceType = .camera self.imagePicker.mediaTypes = [kUTTypeMovie as String] self.imagePicker.allowsEditing = false self.imagePicker.videoMaximumDuration = 30.0 self.imagePicker.delegate = self self.present(self.imagePicker, animated: true, completion: {}) } else { CreateAlert(controller: self, title: "No Camera", message: "Camera is not accessible") } } ,您需要在顶部if presentedController.isKind(of: MPMoviePlayerViewController.self)

import MediaPlayer