[英]How to show MPVideoPlayer in landscape mode and all other views in portrait?
Ive seen similar question asked already so apologies if there were a correct answer I missed. 我已经看到类似的问题已经很抱歉了,如果我错过了正确的答案,我道歉。 My app has to be in portrait mode all the time except when it shows media player which has to be in landscape full screen mode. 我的应用必须始终处于纵向模式,除非它显示必须以横向全屏模式显示的媒体播放器。
Here is how its done: 这是如何完成的:
AppDelegate.m AppDelegate.m
@implementation HCAAppDelegate
+(void) landscapeLock {
HCAAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.screenIsLandscapeOnly= true;
appDelegate.screenIsPortraitOnly = false;
}
+(void) portraitLock
{
HCAAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.screenIsPortraitOnly = true;
appDelegate.screenIsLandscapeOnly = false;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[HCAAppDelegate portraitLock];
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (self.screenIsPortraitOnly) {
return UIInterfaceOrientationMaskPortrait;
} else if (self.screenIsLandscapeOnly) {
return UIInterfaceOrientationMaskLandscape;
} else {
if(self.window.rootViewController) {
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
}
Media player is created as 媒体播放器创建为
-(void) _initPlayer
{
[HCAAppDelegate landscapeLock];
_moviePlayer = [[HCAMoviePlayerController alloc]init];
[_moviePlayer setControlStyle:MPMovieControlStyleDefault];
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
and dismissed as 并以
- (void) _finishPlay
{
...
[_moviePlayer.view removeFromSuperview];
[HCAAppDelegate portraitLock];
}
When player is initialized it goes to landscape, but when its dismissed it still remains in the landscape mode , why the portraitLock method did not work ? 播放器初始化后,它会进入风景模式,但是当其被关闭时,它仍然保留在风景模式下,为什么portraitLock方法不起作用? Thanks! 谢谢!
After looking more into this issue I understood where the bug is. 在深入研究此问题后,我了解了错误的位置。 Will post the answer here to close the case and provide a working example of subj. 将在此处发布答案以结案,并提供subj的有效示例。 Adding mediaPlayer view to the existing view controller [self.view addSubview:_moviePlayer.view]; 将mediaPlayer视图添加到现有视图控制器中[self.view addSubview:_moviePlayer.view]; works but after removing it [_moviePlayer.view removeFromSuperview]; 可以,但是将其删除[_moviePlayer.view removeFromSuperview]; the portraitLock does not affect self.view portraitLock不会影响self.view
The working way is to create a new controller and present / dismiss it: 工作方法是创建一个新的控制器并显示/关闭它:
here is the code: Presenting the view for FullScreen playback. 这是代码:提供全屏播放的视图。 Note that height/width is switched in the line _moviePlayer.view setFrame: 注意,在_moviePlayer.view setFrame行中切换了高度/宽度:
UIViewController *movieVC = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"MoviePlayer"];
movieVC.view = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
[[UIScreen mainScreen] applicationFrame].size.width,
[[UIScreen mainScreen] applicationFrame].size.height)];
[HCAAppDelegate landscapeLock];
[_moviePlayer.view setFrame:CGRectMake(0,
0,
movieVC.view.frame.size.height,
movieVC.view.frame.size.width)];
[movieVC.view addSubview:_moviePlayer.view];
[self.navigationController presentViewController:movieVC animated:YES completion:nil];
Dismissing the view: 关闭视图:
[_moviePlayer stop];
[HCAAppDelegate portraitLock];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.