首页 文章

iPhone无法使用MPMoviePlayerViewController将电影旋转到横向模式

提问于
浏览
8

[更新]

按照建议,我更改了所有父视图控制器以支持所有方向 . 我的app结构如下:AppDelegate> RootViewController> Videos> VideoDetails> MPMoviePlayerViewController .

如果我改变所有这些以支持所有方向,视频将在风景中播放 . 但支持所有方向并不是我想要的,并导致其他问题 . 还有其他工作或我可以做的其他事情吗?

谢谢

[/更新]

我有一个基于肖像的iPhone应用程序,它使用MPMoviePlayerViewController的自定义子类显示视频 . 当用户按下播放时,我创建了这个类的实例,并按模式呈现它,如下所示:

- (IBAction) playPressed:(id)sender {

NSString *filepath = [[NSBundle mainBundle] pathForResource:self.currentVideoModel.videoFileName ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];

// MovieViewController is just a simple subclass of MPMoviePlayerViewController
self.moviePlayerController = [[MovieViewController alloc] initWithContentURL:fileURL]; 

// full screen code.
[self.moviePlayerController.moviePlayer setScalingMode:MPMovieScalingModeFill];
[self.moviePlayerController.moviePlayer setFullscreen:TRUE];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController];

[self presentMoviePlayerViewControllerAnimated:self.moviePlayerController];
}

问题是它在纵向上播放得很好但是当我将iPhone转为横向时,视频仍然以纵向而非横向播放:(应用中的所有视图控制器仅支持纵向 .

我的MPMoviePlayerViewController子类只覆盖以下方法以允许方向更改,但它没有任何影响:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

我甚至尝试以编程方式旋转视频,但绝对没有运气,它始终保持纵向模式 .

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
    return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
    return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
    [self.view setTransform:CGAffineTransformIdentity];
    return true;
}
else return false;

}

1 回答

  • 10

    [编辑]以下解决方案在iOS5上完美运行,但不再适用于iOS6 . 我希望将来能有时间来研究这个问题:([/编辑]

    好的我修好了 . 这完全与我对iOS如何通知应用程序方向变化的误解有关 . 我认为它会广播任何方向更改,但它没有,它遵循您的视图层次结构,由您来告诉任何子视图控制器的方向更改 . 这是我的毁灭 .

    我的应用程序包括以下设置:

    window> RootViewController> tabbar controller> nav controller>视图控制器> MPMoviePlayerViewController

    我将tabbar控制器子类化为仅在纵向模式下返回true . 我从根视图控制器 shouldAutoRotateToOrientation 方法返回了这个 . 这确保了所有视图仅为纵向 .

    然后我使用从RootViewController调用的 presentMoviePlayerViewControllerAnimated 方法以模态方式呈现电影 . 这自动调用自定义MPMoviePlayerViewController的 shouldAutoRotateToOrientation 方法,对于横向和纵向设置为YES :)

相关问题