我正在开发一个通用应用程序,它以非常短的介绍视频开头。所以我只是添加了一个 MPMoviePlayerViewController 并以模态方式呈现它。在 iPhone 上一切正常,即使我以横向模式播放视频。但在 iPad 上,无论设备当前的界面方向如何,视频始终以纵向模式播放。我搜索了几个小时并尝试了很多不同的方法,例如 CGAffineTransformation 或添加新 View ,但似乎没有任何效果。这是我使用的代码:
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 提供的一个简单功能,但有时最简单的事情却是我最需要担心的事情......
最佳答案
我是这样做的。 首先,收听这个通知
[[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];
}
所以诀窍就是使用转换
关于iOS:如何在 iPad 上更改 MPMoviePlayerViewController 的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12563197/