我的整个应用程序仅处于纵向模式,我正在我的应用程序中播放 youtube 视频。对于你管我使用 UIWebview。当用户单击 UIWebview 中的播放按钮时,它会自动启动 MPMoviePlayerController。所以我没有声明任何 MPMoviePlayerController 对象。所以我希望 MPMoviePlayerController 支持纵向和横向。所以请提出建议。
最佳答案
如果您使用 NavigationController
,您可以将其子类化并执行以下操作:
#import "MainNavigationController.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation MainNavigationController
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
@end
然后您应该将您的应用设置为支持所有方向,并且此代码将仅在播放您的电影“MPMoviePlayerController”时允许方向更改。
当调用你的movie
时,你应该发送一个通知,所以如果用户以portrait
以外的任何方向关闭它,它会切换回portrait
.
像这样:
- (IBAction)playButton:(id)sender {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.player.moviePlayer];
NSURL *url = [NSURL URLWithString:videoUrl];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
}
-(void)moviePlaybackDidFinish
{
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
这应该可以为您完成,让我知道进展如何。
关于ios - 设置 MPMoviePlayerController 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26257615/