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

时间:2012-01-24 04:47:46

标签: iphone orientation mpmovieplayercontroller screen-orientation movie

[更新]

按照建议,我更改了所有父视图控制器以支持所有方向。我的app结构如下:AppDelegate> RootViewController>视频> 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 个答案:

答案 0 :(得分:10)

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

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

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

窗口> RootViewController>标签栏控制器>导航控制器>视图控制器> MPMoviePlayerViewController

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

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

相关问题