【问题标题】:YouTube Embedded Player OrientationYouTube 嵌入式播放器方向
【发布时间】:2016-05-07 12:12:12
【问题描述】:

首先要感谢 Google Developers 让我们可以如此轻松地将嵌入式播放器添加到 iOS 项目中。我的问题是如何改变方向。

最初我只在常规设置中将我的项目设置为纵向。这适用于所有视图控制器,但它也适用于 YouTube 播放器。视频按预期播放,但是当我旋转手机时,视频保持纵向。

如果我返回常规设置并将其设置为横向,则播放器会正确旋转(但项目中的所有视图控制器也会正确旋转)。

我在这里阅读了有关如何释放 1 个特定控制器的方向的其他答案,但我只想在全屏观看视频时旋转工作。如果用户决定只在我创建的 UIView 中观看视频,那么我希望锁定方向。

我已按照https://developers.google.com/youtube/v3/guides/ios_youtube_helper 的说明进行操作,因此我需要添加的唯一代码是:

[self.playerView loadWithVideoId:videoSourceID];

就像我说的,视频播放没有问题,视频全屏也没有问题,我只关心方向。

TLDR: 所以基本上,如果用户正在全屏观看 YouTube 视频,我只想解锁“旋转到横向”,应用程序中的其他所有内容都应该锁定为纵向(包括 UIView 显示窗口视频的视图控制器)。感谢收看!

【问题讨论】:

    标签: ios objective-c youtube-api screen-orientation ytplayerview


    【解决方案1】:

    斯威夫特 3.0

    class AppDelegate
        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            guard UIApplication.currentVC.isMovie else { return application.supportedInterfaceOrientations(for: window) }
            return UIInterfaceOrientationMask.allButUpsideDown
        }
    }
    
    
    extension UIApplication {
        struct currentVC {
            static var isMovie: Bool {
                guard let presentedViewController = UIApplication.shared.keyWindow?.rootViewController?.presentedViewController else { return false }
    
                let className = String(describing: type(of: presentedViewController))
    
                return ["MPInlineVideoFullscreenViewController",
                        "MPMoviePlayerViewController",
                        "AVFullScreenViewController"].contains(className)
            }
        }
    }
    
    
    import AVKit
    extension AVPlayerViewController {
        open override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            UIDevice.current.setValue(UIInterfaceOrientationMask.portrait.rawValue, forKey: "orientation")
        }
    }
    

    【讨论】:

      【解决方案2】:

      将此代码粘贴到您的 AppDelegate.m:

      - (UIViewController*)topViewController {
      return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
      }
      
      - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
      // Handling UITabBarController
      if ([rootViewController isKindOfClass:[UITabBarController class]]) {
          UITabBarController* tabBarController = (UITabBarController*)rootViewController;
          return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
      }
      // Handling UINavigationController
      else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
          UINavigationController* navigationController = (UINavigationController*)rootViewController;
          return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
      }
      // Handling Modal views
      else if (rootViewController.presentedViewController) {
          UIViewController* presentedViewController = rootViewController.presentedViewController;
          return [self topViewControllerWithRootViewController:presentedViewController];
      }
      // Handling UIViewController's added as subviews to some other views.
      else 
      {
          for (UIView *view in [rootViewController.view subviews])
          {
              id subViewController = [view nextResponder];    // Key property which most of us are unaware of / rarely use.
              if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
              {
                  return [self topViewControllerWithRootViewController:subViewController];
              }
          }
          return rootViewController;
          }
      }
      

      在以下方法中,您可以决定每个 UIViewController 的方向。把它放在你的AppDelegate.m

      - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
      
      id presentedViewController = [self topViewController];
      NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
      
      if (window && [className isEqualToString:@"FullScreenViewController"]) { //FullScreenViewController should support all orientations. Put the exact name of the viewcontroller displayed when the video is fullscreen
      
          return UIInterfaceOrientationMaskAll;
      } else {
          return UIInterfaceOrientationMaskPortrait; //Every other viewcontroller will support portrait only
      }
      }
      

      您必须检测视频全屏时显示的 UIViewController 的确切名称。您必须在堆栈跟踪中找到它,并将其名称而不是我作为示例编写的“FullScreenViewController”

      【讨论】:

      • 这是一个很棒的解决方案@Metronic,几乎可以完美运行。现在唯一的问题是当您在视频上按 [完成] 并返回到显示原始窗口视频的视图控制器时,如果您仍然以横向方式握住手机,该视图控制器也会显示横向。有没有办法防止这种情况发生?
      • 当然,为防止这种情况发生,您可能需要委托代表检测视频何时从全屏退出并在该方法中输入:[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"方向"];如果找不到此 sn-p 的放置位置,可以将其放在 viewWillAppear
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2014-02-13
      • 2013-03-07
      • 2012-04-11
      • 2014-09-12
      相关资源
      最近更新 更多