首页 文章

如何使MPMoviePlayerViewController以横向模式启动,但仍允许更改方向

提问于
浏览
3

这里有很多问题涉及将电影播放锁定到横向模式,或使用MPMoviePlayerViewController或MPMoviePlayerController支持电影的横向播放 . 我已经拥有横向/纵向功能,我不想将回放锁定到任何一种模式 .

我所拥有的是使用MPMoviePlayerViewController作为文档建议的代码,基本上:

MPMoviePlayerViewController* movieViewController = 
        [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [parentViewController presentMoviePlayerViewControllerAnimated:movieViewController];

我的应用程序主要锁定为纵向模式,但此代码在其自己的视图中以模态方式呈现电影控制器,并支持纵向和横向方向 . 所有这些都非常好 .

这是我的问题 . 我将呈现的视频中有99%是风景视频,上述代码以纵向模式启动电影播放,因为用户(可能)将设备保持在纵向模式 .

我想要的是原生YouTube应用的行为;当您呈现电影控制器时,它首先呈现横向模式,这将提示用户更改其设备的方向 . 如果他们以后想要将其旋转回肖像,则允许他们 . 当电影完成(或解除)时,电影视图控制器将被解除,并且设备应处于纵向模式 .

似乎不可能正确地隐藏状态栏(它与全屏控制相关联,无论启动电影之前'hideStatusBar'的状态如何),所以似乎让状态栏位于正确的位置也需要是部分内容 .

Edited to add debugging notes for status bar orientation :如果我在启动电影之前调用 setStatusBarOrientation:UIInterfaceOrientationLandscapeRight ,则状态栏位于正确的位置,但系统不再以相同的方式调用 shouldAutorotateToInterfaceOrientation .

如果我没有调用 setStatusBarOrientation ,在电影出现之前,我会得到以下一系列电话:

shouldAutorotateToInterfaceOrientation(Portrait)
shouldAutorotateToInterfaceOrientation(Portrait)
shouldAutorotateToInterfaceOrientation(Portrait)
shouldAutorotateToInterfaceOrientation(Right)
shouldAutorotateToInterfaceOrientation(Portrait)

我只回答“是”的权利,电影在LandscapeRight中启动,但状态栏位于错误的位置 . 设备方向的后续更改会产生 shouldAutorotateToInterfaceOrientation 调用'd expect (e.g. if I rotate to Portrait, it asks me if it'可以旋转到纵向) .

如果我打电话 setStatusBarOrientation:UIInterfaceOrientationLandscapeRight ,我会得到以下顺序:

shouldAutorotateToInterfaceOrientation(Right)
shouldAutorotateToInterfaceOrientation(Right)

两个我都回答是 . 状态栏位于正确的位置,但我不再打电话询问有关肖像模式的 shouldAutorotateToInterfaceOrientation . 因此,如果我将设备旋转到右边,然后再回到肖像,然后再回到右边,我看到它调用 shouldAutorotateToInterfaceOrientation(Right) 两次 . 更奇怪的是,如果我将它一直旋转到左方向,我确实得到 shouldAutorotateToInterfaceOrientation(Left) and from then on everything works fine . 哪个最烦人 .

我认为这肯定是iOS方面的一个错误,这就是YouTube应用程序不使用动画UI旋转效果的原因 . 相反,它隐藏了全屏控件,包括状态栏,在后台无形旋转,然后重新显示控件 .

2 回答

  • 4

    我建议启动一个计时器;首先只允许 shouldAutorotateToInterfaceOrientation: 中的横向(这将强制旋转到横向),然后在(比方说)5秒后允许所有方向 .

    首先,您需要子类化MPMoviePlayerViewController或在其自己的视图中创建MPMoviePlayerController . 额外的代码看起来像这样:

    // On load, init or similar method
    BOOL showLandscapeOnly = YES;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@(allowAllOrientations) userInfo:nil repeats:NO];
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      if (showLandscapeOnly == YES)
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
                interfaceOrientation == UIInterfaceOrientationLandscapeRight);
      else
        return YES;
    }
    
    - (void)allowAllOrientations {
      showLandscapeOnly = NO;
    }
    

    我认为这将完全符合你所追求的目标;在显示模态视频控制器时,它会将方向翻转为横向,因为这是唯一支持的方向 . 但如果用户将其恢复为肖像,那么它将会很好地响应 .

    您也可以尝试尝试其他时间间隔;也许2秒会更好?建议您查看测试用户主题的作用 . 他们可能很快就会旋转设备 .

    希望有所帮助!

  • 0

    您将不得不旋转电影的帧,然后告诉应用委托在听到旋转消息时不自行旋转 .

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return NO;
    }
    

相关问题