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

标签 ios ipad movieplayer

我正在开发一个通用应用程序,它以非常短的介绍视频开头。所以我只是添加了一个 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/

相关文章:

android - build.phonegap.com 是否有任何开源替代品?

ios - iOS Touches开始的后续触摸只有在第一触摸被移动时才能到达

iphone - 如何将介绍影片添加到我的应用程序?

iphone - 电影播放完毕后在电影播放器​​中显示控件

video - 从 NSDocumentDirectory 播放视频

ios - 使用Objective-C压缩文件

ios - 我如何解决 IOS 版本 React Native 应用程序中的 EXC_CRASH (SIGABRT) 问题?

html - NSString 移除 html 标签,保留 <Text in angle brackets>

iphone - 如何在 UIAlertView 中制作文本字段 "become first responder"

iphone - 我的 View 显示 y20,尽管其框架设置为 y=0。旋转后它会恢复到 y=0