iOS:如何在iPad上更改MPMoviePlayerViewController的方向?

时间:2012-09-24 10:30:57

标签: ios ipad movieplayer

我正在开发一个以简短的介绍视频开头的通用应用程序。所以我只需添加一个MPMoviePlayerViewController并以模态方式呈现它。在iPhone上一切正常,即使我以横向模式播放视频。但是在iPad上,无论设备当前具有什么接口方向,视频总是以纵向模式播放。我搜索了几个小时,尝试了很多不同的东西,如CGAffineTransformation或添加一个新的视图,但似乎没有任何工作。以下是我使用的代码:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];  

    self.startupController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    [self.startupController.moviePlayer setControlStyle:MPMovieControlStyleNone];
    [self.startupController.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [self.startupController.moviePlayer setFullscreen:YES];        
    [self presentModalViewController:self.startupController animated:NO];

你知道如何解决这个问题吗?在我看来,这应该是Apple提供的一个简单功能,但有时最简单的事情是我最担心的事情......

1 个答案:

答案 0 :(得分:3)

我是这样做的。 首先,听听这个通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

然后执行:

- (void)detectOrientation {
    NSLog(@"handling orientation");
    [self setMoviePlayerViewOrientation:[[UIDevice currentDevice] orientation]];
}


- (void)setMoviePlayerViewOrientation:(UIDeviceOrientation)orientation {
if (lwvc != nil)
    return;
if (moviePlayerController.playbackState == MPMoviePlaybackStateStopped) return;
//Rotate the view!
CGFloat degree = 0;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
switch (orientation) {
    case UIDeviceOrientationPortrait:
    case UIDeviceOrientationPortraitUpsideDown:
        degree = 0;
        moviePlayerController.view.bounds = CGRectMake(0, 0, 320, height);
        break;
    case UIDeviceOrientationLandscapeLeft:
        degree = 90;
        moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
        break;
    case UIDeviceOrientationLandscapeRight:
        degree = -90;
        moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
        break;
    default:
        break;
}
lastOrientation = orientation;
CGAffineTransform cgCTM = CGAffineTransformMakeRotation((degree) * M_PI / 180);
moviePlayerController.view.transform = cgCTM;
[UIView commitAnimations];
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];
}

所以诀窍就是使用转换