首页 文章

在全屏幕上强制横向方向MPMoviePlayerController在退出全屏时阻止正确旋转

提问于
浏览
14

我有一个支持所有界面方向的iPhone应用程序(iOS6) . 但是,当MPMoviePlayerController正在播放全屏视频时,应仅支持横向 .

我在Stack Overflow上找到了以下解决方案,它可以工作 .

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

...

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.landscapeOnlyOrientation) {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskAll;
}

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
    self.landscapeOnlyOrientation = YES;
}

- (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
    self.landscapeOnlyOrientation = NO;
}

然而,一个恼人的问题仍然存在,即如果视频以纵向方向退出全屏(在强制风景中播放之后),则底层视图不会向后旋转 . 我必须手动将设备旋转到横向并返回到纵向以触发更新方向 . 有没有什么方法可以以编程方式触发这种更新?

以下一组截图应说明我的意思:

enter image description here

enter image description here

enter image description here

注意:由于各种原因,无法使用MPMoviePlayerViewController .

8 回答

  • 2

    大家好我有同样的问题我解决了 -

    这是我的完整代码....

    您需要先在appdelegate中进行更改:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here
    {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
    }
    

    注册全屏控制通知:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
                                                 name:MPMoviePlayerWillEnterFullscreenNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];
    

    然后在播放器控制器中添加代码行:

    - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification
    {
    dispatch_async(dispatch_get_main_queue(), ^
                   {
                       self.allowRotation = YES;
                   });
    }
    
    
    
    - (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification
    {
    self.allowRotation = NO;
    [self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
    
    dispatch_async(dispatch_get_main_queue(), ^
                   {
    
                       //Managing GUI in pause condition
                           if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused)
                       {
                           [self.moviePlayerController pause];
                           if (self.playButton.selected)
                               self.playButton.selected = NO;
                       }
                       self.view.transform = CGAffineTransformMakeRotation(0);
                       [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
                       self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
                   });
    }
    

    此代码在iOS6和iOS7中测试正常 . 谢谢 :)

    如果有任何问题,请告诉我.....

  • 0

    您需要子类化并提供格局作为支持的界面方向 .

    @interface MyMovieViewController : MPMoviePlayerViewController
    @end
    
    @implementation MyMovieViewController
    
    - (BOOL)shouldAutorotate 
    {
        return YES;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations 
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
    }
    
    @end
    
  • -1

    您可以尝试“强制”方向刷新,让系统为您处理正确的方向:

    - (void)forceOrientationRefresh
    {
        // Force orientation refresh
        [self presentModalViewController:[UIViewController new]
                                animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
    

    这是hack-ish但它的确有效 .

  • 0

    这可能听起来很疯狂,但是,您可以尝试在打开视频视图控制器之前在本地保存最后一个方向,然后使用 application:supportedInterfaceOrientationsForWindow: 返回保存的方向并强制视图控制器保持在其上而不是旋转 .

  • 11

    您可以像这样以编程方式更改您的方向

    -(void)viewDidAppear:(BOOL)animated
    {
    
         if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
            if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
            {
                objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft );
    
            }
        }
    
    }
    

    别忘了添加 #import <objc/message.h>

  • 5

    我认为您可以注册viewcontroller以获取设备方向并强制调用viewcontroller的方向方法 .

  • 0

    您使用supportedIterfaceOrientationsForWindow然后查找:MPInlineVideoFullscreenViewController . 找到正确的视图控制器有点棘手,但它确实有效 .

    以下是示例代码:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        if ([NSStringFromClass([self.window.rootViewController.presentedViewController.presentedViewController class]) isEqualToString:@"MPInlineVideoFullscreenViewController"]){
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
        return UIInterfaceOrientationMaskLandscape;
    }
    
  • 0

    你需要为iOS7添加这个代码,它完美而简单

    -(NSUInteger)supportedInterfaceOrientations {
    
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    
    .... creating a player
    MPMoviePlayerViewController *mp =[[MPMoviePlayerViewController alloc] initWithContentURL:url];
    ...make settings and present it
    [self presentMoviePlayerViewControllerAnimated:mp];
    

相关问题