iphone - 强制MPMoviePlayerController在横向模式下播放视频
我有一个应用程序仅适用于纵向模式,但是当用户播放视频时,我希望它以全屏横向模式播放(视频播放器在纵向中看起来不太好模式)。我在玩这样的:iphone - 强制MPMoviePlayerController在横向模式下播放视频
[self.view addSubview:myMoviePlayer.view];
[self.myMoviePlayer setFullscreen:YES animated:YES];
[self.myMoviePlayer play];
什么是实现这一目标的最佳方式是什么?
阅读这篇文章:
的主要思想是:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)];
对于全屏播放使用MPMoviePlayerViewController,然后让它启动,并以横向格式播放使用“shouldAutorotateToInterfaceOrientation “在MPMoviePlayerViewController类上的方法。
它看起来像这样:
[yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
我有同样的情况,但我使用的是iOS 6和基于NavController项目。让我感兴趣的是ViewController,它托管着我不想旋转的MPMoviePlayerController,但我希望它内部的视频能够旋转。
我基于设备取向只是需要手动旋转的MPMoviePlayerController。 _videoView是ViewController的MPMoviePlayerController属性。例如,我只是将所需的宽度和高度硬编码为16x9长宽比,因为我打算将此视频上传到YouTube。
- (void)updateVideoRotationForCurrentRotationWithAnimation:(bool)animation
{
CGSize containerSize = _videoView.frame.size; // Container NOT rotated!
float videoWidth = 384; // Keep 16x9 aspect ratio
float videoHeight = 216;
if(animation)
{
[UIView beginAnimations:@"swing" context:nil];
[UIView setAnimationDuration:0.25];
}
switch(self.interfaceOrientation)
{
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(90));
// This video player is rotated so flip width and height, but the container view
// isn't rotated!
[m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoHeight)/2,
(containerSize.height-videoWidth)/2,
videoHeight,
videoWidth)];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(0));
// This video player isn't rotated
[m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoWidth)/2,
(containerSize.height-videoHeight)/2,
videoWidth,
videoHeight)];
break;
}
if(animation)
[UIView commitAnimations];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self updateVideoRotationForCurrentRotationWithAnimation:YES];
}
我也呼吁updateVideoRotationForCurrentRotationWithAnimation视图后没有出现因此它具有正确的初始方向。
我的应用程序很简单,使用UINavigationController作为根视图控制器,它显示主导航屏幕和一些细节屏幕。由于布局约束,主屏幕不支持旋转,并且为了保持细节屏幕(只是简单地推送到导航控制器的堆栈)的一致性,请不要这样做。但是,某些详细信息屏幕包含视频链接,并且视频需要以纵向模式显示。该应用程序使用MPMoviePlayerController以模态方式显示玩家。
我刚刚有一个类似的问题,并尝试了上面的解决方案,将仿射变换应用到MPMoviePlayerController的视图,但问题是状态栏继续以纵向模式显示,并重叠模式电影播放器视图在左侧,如果根据上面的旋转查看)。我试了几件事情都无济于事:
隐藏状态栏。这是行不通的,因为玩家存在于自己的世界中,并继续并呈现状态栏。我无法找到一种方法让它消失。
显式设置状态栏方向。我不确定这究竟是为什么不起作用,但我怀疑这是因为我在我的info.plist中指出,只有肖像被支持,因此被切断了进行更改。
净净,上面并没有为我工作。同样,苹果公司警告开发人员将MPMoviePlayerController视为不透明,并且(特别是对于变换方法),我违反了这一规定。
最后,我发现了一个简单的解决方案是为我工作:
在info.plist中,我指出,所有的方向(除倒挂,所以标准的iPhone成语)的支持。
UINavigationController的子类,并重写适当shouldAutorotate方法,因此只有人像支持(见this solution,除其他外,如何做到这一点iOS中< 6和iOS6的同时)。
这工作,因为:
虽然我已经指出,自转由应用程序的支持,我就把它砍下来,在里面包含了所有的我呈现的意见UINavigationController的子类。 ..所以一切仍然是肖像,除了:
MPMoviePlayerController模态地呈现,导航控制器上方,并居住在自己的世界。因此,可以*地关注info.plist中的内容,并自行旋转。
有一堆关于如何模态呈现玩家的例子,但对于快速参考,这里是我的代码:
- (void)presentModalMediaPlayerForURL:(NSURL *)inURL
{
NSLog(@"Will play URL [%@]", [inURL description]);
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:inURL];
[player.view setBounds:self.view.bounds];
[player.moviePlayer prepareToPlay];
[player.moviePlayer setFullscreen:YES animated:YES];
[player.moviePlayer setShouldAutoplay:YES];
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[self presentModalViewController:player animated:YES];
[player release];
}
我用下面的代码只有横向模式Movieplayer工作。
NSURL *movieURL = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"]; // sample url
MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
// Logic for play movie in landscape
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieController.moviePlayer.view setTransform: landscapeTransform];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer prepareToPlay];
[movieController.moviePlayer play];
这已经在IOS 6中被弃用了,它实际上并不会让播放器旋转。 – 2012-10-28 04:44:38