如何在横向模式下显示MPVideoPlayer以及在纵向中显示所有其他视图?

时间:2021-10-19 23:03:23

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 ? Thanks!

玩家初始化时会转到横向,但是当它被解雇时它仍然处于横向模式,为什么portraitLock方法不起作用?谢谢!

1 个解决方案

#1


0  

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. Adding mediaPlayer view to the existing view controller [self.view addSubview:_moviePlayer.view]; works but after removing it [_moviePlayer.view removeFromSuperview]; the portraitLock does not affect self.view

在深入研究这个问题之后,我了解了bug的位置。将在这里发布答案以关闭案例并提供subj的工作示例。将mediaPlayer视图添加到现有视图控制器[self.view addSubview:_moviePlayer.view];有效但删除后[_moviePlayer.view removeFromSuperview]; 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:

这是代码:显示FullScreen播放的视图。请注意,高度/宽度在_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];

#1


0  

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. Adding mediaPlayer view to the existing view controller [self.view addSubview:_moviePlayer.view]; works but after removing it [_moviePlayer.view removeFromSuperview]; the portraitLock does not affect self.view

在深入研究这个问题之后,我了解了bug的位置。将在这里发布答案以关闭案例并提供subj的工作示例。将mediaPlayer视图添加到现有视图控制器[self.view addSubview:_moviePlayer.view];有效但删除后[_moviePlayer.view removeFromSuperview]; 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:

这是代码:显示FullScreen播放的视图。请注意,高度/宽度在_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];