0

我正在开发一个以非常短的介绍视频开头的通用应用程序。所以我只需添加一个 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 提供的一个简单功能,但有时最简单的事情是我最担心的事情......

4

1 回答 1

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];
}

所以诀窍就是使用转换

于 2012-09-24T10:48:39.980 回答