【问题标题】:MPMoviePlayerViewController | Allow landscape modeMPMoviePlayerViewController |允许横向模式
【发布时间】:2013-06-21 01:03:08
【问题描述】:

我正在尝试在我的应用中流式传输视频。我找到的方法是:

NSURL *theMovieURL = [NSURL URLWithString:self.data.trailer];
        if (theMovieURL)
        {
            self.movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:theMovieURL];
            [self presentMoviePlayerViewControllerAnimated:self.movieController];
            [self.movieController.moviePlayer play];
        }

我不确定它是否是最传统的,但它确实有效。

问题是我不知道如何允许横向模式,仅用于视频。我应该使用 shouldAutorotateshouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 之类的东西吗?如何使用?

仅供参考,整个应用只允许纵向模式。

感谢您的帮助。

【问题讨论】:

    标签: ios mpmovieplayercontroller landscape


    【解决方案1】:

    shouldAutoRotate 自 iOS 6 起已弃用,除非您打算使用

    相反,您需要覆盖 supportedInterfaceOrientationspreferredInterfaceOrientationForPresentation 方法。

    在这种情况下,如果您不想子类化媒体播放器,您可以像这样覆盖应用委托中的方法:

    - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
        {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
        else
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    【讨论】:

    • 检查presentedViewController是否被关闭(isBeingDismissed属性),否则当前viewcontroller会以横向模式显示
    【解决方案2】:

    @peko 说的是正确的方法。当用户退出全屏视频时,此方法再次使用MPMoviePlayerViewController 类调用。你应该检查它是否被解雇,如果你不这样做,当用户退出视频时,主窗口将保持横向模式,你也忘记了MPInlineVideoFullscreenViewController类。当您使用嵌入播放器(不是全屏)时,它会使用该类名调用。

    我就是这样做的。它对我有用。

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx
    {
        if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] ||
        [[self.window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")])
        {
            if ([self.window.rootViewController presentedViewController].isBeingDismissed)
            {
                return UIInterfaceOrientationMaskPortrait;
            }
            else
            {
                return UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
        else
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    【讨论】:

      【解决方案3】:

      电影播放器​​可能不是第一个呈现的视图控制器,所以你应该这样做

      - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
          UIViewController* playerController = self.window.rootViewController.presentedViewController;
          while (playerController && ![playerController isKindOfClass:[MPMoviePlayerViewController class]]) {
              playerController = playerController.presentedViewController;
          }
          if (playerController && !playerController.isBeingDismissed) {
              return UIInterfaceOrientationMaskAllButUpsideDown;
          } else {
              return UIInterfaceOrientationMaskPortrait;
          }
      }
      

      尽管MPMoviePlayerViewController 已被弃用,因此您应该改用AVPlayerViewController 并在此循环中检查其类。

      【讨论】:

        【解决方案4】:

        到目前为止,最好的方法是实现这个 AppDelegate 函数并检查 rootViewController 是否有 AVPlayerViewControllerMPMoviePlayerViewController 类型的子元素,然后返回 [.portrait, .landscapeLeft, .landscapeRight] 否则返回 .portrait

        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            if let _ = UIApplication.shared.keyWindow?.rootViewController?.childViewControllers.first as? AVPlayerViewController {
                return [.portrait, .landscapeLeft, .landscapeRight]
            }
            return .portrait
        }
        

        您应该检查UIApplicationkeyWindow,因为Apple 在另一个UIWindow 中显示了这个viewController,所以如果您尝试检查AppDelegate 中声明的窗口,这将不起作用所以要小心。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多