【问题标题】:Forcing landscape orientation on fullscreen MPMoviePlayerController prevents correct rotation when exiting fullscreen在全屏 MPMoviePlayerController 上强制横向可防止在退出全屏时正确旋转
【发布时间】:2014-03-21 14:14:38
【问题描述】:

我有一个支持所有界面方向的 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;
}

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

下面一组截图应该能说明我的意思:

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

【问题讨论】:

  • 几个月前我就这个问题向 Apple 提交了一个错误。我建议你这样做。问题是没有参考底层视图控制器的方向方法。
  • 对解决方法有什么建议吗?
  • 没有。您可以尝试阻止使用全屏模式。或者只是不使用 MPMoviePlayerController。基本上,这只是 Apple 的一个很大的不一致之处,开发人员需要坚持下去,直到他们修复它。
  • 你检查过这个答案了吗?附有一个示例项目,可能会对您有所帮助。 stackoverflow.com/questions/15947349/…

标签: ios iphone objective-c mpmovieplayercontroller uiinterfaceorientation


【解决方案1】:

大家好,我遇到了同样的问题,我解决了 -

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

你需要先改变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 中进行了测试,工作正常。谢谢:)

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

【讨论】:

  • 这里要检查什么条件?if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//把你的条件放在这里,现在玩什么意思?
  • 嗨 Vijay,感谢您的代码。删除 WillExitFullScreen 方法中的 dispatch_async 代码后,它工作正常。能不能简单介绍一下为什么放了没用的代码,什么是TypeVideo??
  • 感谢萨蒂亚的评论。我使用 dispatch_async 来访问主线程,因为在我的情况下,当我进入横向模式时,我以前的 GUI 在动画时会失真。就我而言,我在同一个播放器上播放音频和视频,每当视频开始播放时,它就会全屏显示,而不是音频歌曲。所以我用 TypeVideo 来播放视频歌曲。
  • 抱歉 karthikeyan 回复晚了。 [[[NowPlaying sharedManager] playerViewController] allowRotation] 在我的情况下 PlayerViewController 获取 Landscape 以防我进入全屏播放器模式。所以在 -> '- (void)moviePlayerWillEnterFullscreenNotification' 通知方法调用 playerview 控制器时,我说是现在允许所有旋转通过 'self.allowRotation = YES;'如果您对此不清楚,请告诉我。谢谢。
【解决方案2】:

您需要子类化并提供横向作为支持的界面方向。

@interface MyMovieViewController : MPMoviePlayerViewController
@end

@implementation MyMovieViewController

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

@end

【讨论】:

    【解决方案3】:

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

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

    它有点像 hack-ish,但它可以工作。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        -(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>

        【讨论】:

          【解决方案6】:

          我认为您可以为设备方向注册您的视图控制器并强制调用视图控制器的方向方法。

          【讨论】:

            【解决方案7】:

            您使用supportedIterfaceOrientationsForWindow 然后查找:MPInlineVideoFullscreenViewController。找到正确的视图控制器有点棘手,但它可以工作。

            这里是示例代码:

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

            【讨论】:

              【解决方案8】:

              您需要为 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];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2012-01-15
                • 2011-12-28
                • 1970-01-01
                • 1970-01-01
                • 2015-09-30
                • 2016-09-12
                • 2023-04-03
                相关资源
                最近更新 更多