我有一个仅限肖像模式的应用程序,但是当用户播放视频时,我希望它以全屏横向模式播放(视频播放器在纵向模式下看起来不太好) . 我这样玩:
[self.view addSubview:myMoviePlayer.view]; [self.myMoviePlayer setFullscreen:YES animated:YES]; [self.myMoviePlayer play];
实现这一目标的最佳方法是什么?
阅读这篇文章:
http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html
主要想法是:
[[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)];
我的应用程序非常简单,UINavigationController作为根视图控制器,显示主导航屏幕和一些细节屏幕 . 由于布局限制,主屏幕不支持旋转,并且为了保持一致性,细节屏幕(简单地按到导航控制器的堆栈上)也不支持 . 但是,某些详细信息屏幕包含视频链接,视频需要以纵向模式显示 . 该应用程序使用MPMoviePlayerController以模态方式显示播放器 .
我刚刚遇到了类似的问题,并尝试了上面的解决方案,它将仿射变换应用于MPMoviePlayerController的视图,但问题是状态栏继续显示为纵向模式,并与模态电影播放器视图重叠(在左侧,如果根据上面的轮换观察) . 我尝试了几件事无济于事:
隐藏状态栏 . 这不起作用,因为玩家存在于自己的世界中,并继续前进并呈现状态栏 . 我找不到办法让它消失 .
显式设置状态栏方向 . 我不确定为什么这不起作用,但我怀疑是因为我在我的info.plist中指出只支持肖像,因此切断了更改 .
净网,以上对我不起作用 . 同样,b / c Apple告诫开发人员将MPMoviePlayerController视为不透明,并且(特别是使用转换方法)我违反了这一点 .
最后,我找到了一个对我有用的简单解决方案:
在info.plist中,我表示支持所有方向(倒置除外,因此标准的iPhone成语) .
子类UINavigationController,并覆盖相应的shouldAutorotate方法,以便仅支持Portrait(有关如何在iOS <6和iOS6中同时执行此操作,请参阅this solution等) .
这是因为:
虽然我'd indicated that autorotation is supported by the app, I cut it off at the UINavigationController subclass which contains all of the views I'渲染...所以一切都是肖像,除了:
MPMoviePlayerController以模态方式呈现在NavigationController的顶部,并且存在于它自己的世界中 . 因此,可以自由地关注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]; }
对于全屏播放,使用MPMoviePlayerViewController然后使其以横向格式启动和播放使用MPMoviePlayerViewController类上的“shouldAutorotateToInterfaceOrientation”方法 .
它看起来像这样:
[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,因此它具有正确的初始方向 .
我使用以下代码来处理Movieplayer的ONLY LANDSCAPE模式 .
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];
5 回答
阅读这篇文章:
http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html
主要想法是:
我的应用程序非常简单,UINavigationController作为根视图控制器,显示主导航屏幕和一些细节屏幕 . 由于布局限制,主屏幕不支持旋转,并且为了保持一致性,细节屏幕(简单地按到导航控制器的堆栈上)也不支持 . 但是,某些详细信息屏幕包含视频链接,视频需要以纵向模式显示 . 该应用程序使用MPMoviePlayerController以模态方式显示播放器 .
我刚刚遇到了类似的问题,并尝试了上面的解决方案,它将仿射变换应用于MPMoviePlayerController的视图,但问题是状态栏继续显示为纵向模式,并与模态电影播放器视图重叠(在左侧,如果根据上面的轮换观察) . 我尝试了几件事无济于事:
隐藏状态栏 . 这不起作用,因为玩家存在于自己的世界中,并继续前进并呈现状态栏 . 我找不到办法让它消失 .
显式设置状态栏方向 . 我不确定为什么这不起作用,但我怀疑是因为我在我的info.plist中指出只支持肖像,因此切断了更改 .
净网,以上对我不起作用 . 同样,b / c Apple告诫开发人员将MPMoviePlayerController视为不透明,并且(特别是使用转换方法)我违反了这一点 .
最后,我找到了一个对我有用的简单解决方案:
在info.plist中,我表示支持所有方向(倒置除外,因此标准的iPhone成语) .
子类UINavigationController,并覆盖相应的shouldAutorotate方法,以便仅支持Portrait(有关如何在iOS <6和iOS6中同时执行此操作,请参阅this solution等) .
这是因为:
虽然我'd indicated that autorotation is supported by the app, I cut it off at the UINavigationController subclass which contains all of the views I'渲染...所以一切都是肖像,除了:
MPMoviePlayerController以模态方式呈现在NavigationController的顶部,并且存在于它自己的世界中 . 因此,可以自由地关注info.plist中的内容,并自行旋转 .
有很多关于如何以模态方式呈现播放器的示例,但为了快速参考,这是我的代码:
对于全屏播放,使用MPMoviePlayerViewController然后使其以横向格式启动和播放使用MPMoviePlayerViewController类上的“shouldAutorotateToInterfaceOrientation”方法 .
它看起来像这样:
我有相同的情况,但我正在使用iOS 6和基于NavController的项目 . 让有趣的是ViewController托管MPMoviePlayerController我不想旋转,但我想让它内的视频旋转 .
我只需根据设备方向手动旋转MPMoviePlayerController . _videoView是ViewController的MPMoviePlayerController属性 . 例如,我只是将所需的宽度和高度硬编码为16x9宽高比,因为我打算将此视频上传到youtube .
我也在视图确实出现后调用updateVideoRotationForCurrentRotationWithAnimation,因此它具有正确的初始方向 .
我使用以下代码来处理Movieplayer的ONLY LANDSCAPE模式 .