简体   繁体   English

如何以横向模式显示MPVideoPlayer并以纵向显示所有其他视图?

[英]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.

相关问题 XCode:如何在横向和纵向模式之间更改视图布局 - XCode: How to change layout of views between landscape and portrait mode 如何使用自动版式在纵向模式下垂直堆叠视图以及在横向模式下水平堆叠视图? - How to use Auto Layout to stack views vertically in portrait mode and horizontally in landscape mode? 如何在纵向和横向中提供两种不同的视图? - How to provide two different views in portrait and landscape? Xcode 4.5:如何修复我的文本视图,以便它们在iPad上以纵向和横向模式正确显示? - Xcode 4.5: How do I fix my text views so they display properly in portrait and landscape mode on an iPad? 方向肖像和风景模式 - Orientation portrait and landscape mode 如何将视图从纵向模式旋转到横向模式 - How to rotate view from portrait to landscape mode 首次启动应用程序时,如何仅在纵向和横向模式下在 viewDidAppear 中显示自定义子视图一次? - How to show custom subviews in viewDidAppear both in portrait and landscape mode only once when the app is launched for the first time? iOS纵向和横向模式 - ios portrait and landscape mode 在纵向模式下保持一个视图控制器,在横向模式下保持另一个视图控制器 - Maintain one view controller in Portrait and other in Landscape mode iOS 如何在flutter中显示横向和纵向的不同布局 - How to show different layout for landscape and portrait in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM