MPMoviePlayerViewController | 允许横向模式

Nic*_*Roy 19 landscape mpmovieplayercontroller ios

我正在尝试在我的应用中流式传输视频.我发现的方法是:

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

我不确定它是否是最常规的,但它确实有效.

问题是我无法弄清楚如何仅为视频设置横向模式.我应该使用类似shouldAutorotate或者shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation,怎么样?

仅供参考,整个应用程序仅允许纵向模式.

谢谢你的帮助.

小智 34

shouldAutoRotate 从iOS 6开始被弃用,除非你的目标是<6,否则应该避免使用.

相反,你想要覆盖supportedInterfaceOrientationspreferredInterfaceOrientationForPresentation方法.

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

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

  • 检查presentViewController是否被解除(isBeingDismissed属性),否则呈现的viewcontroller将显示在landscapemode中 (12认同)

woh*_*ras 17

@peko说正确的方法.当用户退出全屏视频时,此方法再次使用MPMoviePlayerViewControllerclass 调用.你应该检查它是否被解雇,如果你不这样做,当用户退出视频时,主窗口将保持横向模式,你也忘了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;
    }
}
Run Code Online (Sandbox Code Playgroud)