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

时间:2014-10-31 16:04:42

标签: ios objective-c iphone

我已经看过类似的问题,如果我错过了正确答案,那么道歉。 我的应用程序必须始终处于纵向模式,除非它显示必须处于横向全屏模式的媒体播放器。

以下是它的完成方式:

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;
    }
}

媒体播放器创建为

-(void) _initPlayer
{
    [HCAAppDelegate landscapeLock];

    _moviePlayer = [[HCAMoviePlayerController alloc]init];
    [_moviePlayer setControlStyle:MPMovieControlStyleDefault];

    _moviePlayer.shouldAutoplay = YES;

    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:YES];
}

并被解雇为

- (void) _finishPlay
{
...
    [_moviePlayer.view removeFromSuperview];
    [HCAAppDelegate portraitLock];
}

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

1 个答案:

答案 0 :(得分:0)

在深入研究这个问题之后,我明白了bug的位置。将在这里发布答案以关闭案例并提供subj的工作示例。 将mediaPlayer视图添加到现有视图控制器     [self.view addSubview:_moviePlayer.view]; 工作但删除后     [_moviePlayer.view removeFromSuperview]; portraitLock不会影响self.view

工作方式是创建一个新的控制器并显示/解除它:

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

驳回观点:

[_moviePlayer stop];
[HCAAppDelegate portraitLock];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];