iPad中的MPMoviePlayerController全屏怪癖

iPad中的MPMoviePlayerController全屏怪癖,ipad,fullscreen,mpmovieplayercontroller,statusbar,screen-orientation,Ipad,Fullscreen,Mpmovieplayercontroller,Statusbar,Screen Orientation,我想在视图控制器中显示MPMoviePlayerController,并让用户使用默认控件切换全屏,比如YouTube应用程序。我在一个简单的示例中使用以下代码: - (void)viewDidLoad { [super viewDidLoad]; self.player = [[MPMoviePlayerController alloc] init]; self.player.contentURL = theURL; self.player.view.frame

我想在视图控制器中显示MPMoviePlayerController,并让用户使用默认控件切换全屏,比如YouTube应用程序。我在一个简单的示例中使用以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.player = [[MPMoviePlayerController alloc] init];
    self.player.contentURL = theURL;
    self.player.view.frame = self.viewForMovie.bounds;
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.viewForMovie addSubview:player.view];
    [self.player play];
}
在用户将视频设置为全屏、旋转设备并轻触屏幕之前,此功能一直运行良好。状态栏显示在错误的位置,如下面的屏幕截图所示

我正在使用iPad的模板选项卡栏应用程序。我只添加了上面的viewDidLoad、视图变量和XIB中的UIView来显示电影播放器


我做错了什么?

是shouldAutorotateToInterfaceOrientation:interfaceOrientation为所有支持的方向返回YES

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } -(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{ 返回YES; }
如果您提供了更多的代码,这会有所帮助。

您的UI是否使用interface builder?如果是这样,请确保在“视图属性检查器”中将视图的方向设置为“横向”。

也有同样的问题,只是花了半天时间进行整理。由于iPad是纵向的,每当我使用示例代码(或我在网上能找到的任何代码)开始一段视频时,视频和控制栏的格式都是纵向的,因此在屏幕上到处都是

不管怎么说,下面这些对我来说很有用

  /* Call the code like below:
        int iLandscape;
        if( newOrientation==UIInterfaceOrientationLandscapeLeft || newOrientation==UIInterfaceOrientationLandscapeRight )
             iLandscape=1;

        [self PlayVideo:iLandscape fullscreen:1]
    */
        //////////////////////////////////////////////////////////////////////////
        - (void)PlayVideo:(int)iLandscape fullscreen:(int)iFullScreen 
        {
            NSString *url = [[NSBundle mainBundle] pathForResource:@"myvideofile" ofType:@"m4v"];

        if( iFullScreen==0 )
        {
            MPMoviePlayerController *player2 = 
                [[MPMoviePlayerController alloc] 
                    initWithContentURL:[NSURL fileURLWithPath:url]];

            [[NSNotificationCenter defaultCenter] 
                addObserver:self
                   selector:@selector(movieFinishedCallback:)
                       name:MPMoviePlayerPlaybackDidFinishNotification
                     object:player2];

            //---play partial screen---
            player2.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            [self addSubview:player2.view];
            //---play movie---
            [player2 play];
        }   
        else
        {
            MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:[NSURL fileURLWithPath:url]];

            [[NSNotificationCenter defaultCenter] 
                addObserver:self
                   selector:@selector(movieFinishedCallback:)
                       name:MPMoviePlayerPlaybackDidFinishNotification
                     object:[playerViewController moviePlayer]];

            if( iLandscape )
            {
                playerViewController.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            }
            [self addSubview:playerViewController.view];
            //play movie
            MPMoviePlayerController *player = [playerViewController moviePlayer];
            player.scalingMode=MPMovieScalingModeAspectFit;
            [player play];
        }
    }


    //////////////////////////////////////////////////////////////////////////
    - (void) movieFinishedCallback:(NSNotification*) aNotification 
    {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    
        [player autorelease];    
        [player.view removeFromSuperview];
    }

你解决这个问题了吗

[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

这段代码可能会帮助您。

是的,我也遇到了这个问题。这显然是MPMoviePlayerController本身的一个bug

我在应用程序中确定的解决方法是,在退出全屏模式时,自己更正状态栏:

- (void)playerDidExitFullscreen:(NSNotification *)notification {
    MPMoviePlayerController *moviePlayer = (MPMoviePlayerController *) notification.object;

    if (moviePlayer == self.player) {
        UIApplication *app = [UIApplication sharedApplication];
        if (app.statusBarOrientation != self.interfaceOrientation) {
            [app setStatusBarOrientation:self.interfaceOrientation animated:NO];
        }
    }
}
这并不能解决全屏模式下的问题,但会在之后解决

当然,请注意,该功能需要添加到通知中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
找到了

我也有同样的问题——这就是我所做的。我建议将代码一个一个地添加到您的项目中,以了解它是如何工作的

首先,我把事情放在肖像模式上

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
然后我把电影推到状态栏上。注意-这假设视频的纵横比为4x3

theVideo = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath : path]];
float aspectRatio = (3.0f/4.0f);
float theMovieHeight = [self view].bounds.size.width * aspectRatio;
[[theVideo view] setFrame:(CGRectMake(0, [self view].bounds.size.height - theMovieHeight, [self view].bounds.size.width, theMovieHeight ))]; 
然后,在应用程序启动的地方(在我的项目中,它位于
didfinishlaunchwithoptions
函数中),无论如何,您只需要访问窗口对象

float aspectRatio = (3.0f/4.0f);
float theMovieHeight = self.window.bounds.size.width * aspectRatio;
float theSpaceAboveTheMovie = self.window.bounds.size.height - theMovieHeight;
float whereTheMovieShouldBeCentered = (self.window.bounds.size.height - theMovieHeight) / 2;

CGAffineTransform theTransform = CGAffineTransformMakeTranslation(0,0);

theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);

[self.window setTransform:theTransform];
请记住,仿射变换是按相反的顺序进行的。因此,如果您想查看每个变换都在做什么(我建议您应该这样做),请注释掉前三个

在这里,您应该可以看到页面中央的电影和状态栏

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
//  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
//  theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
然后是前两个

在这里,您应该看到电影和状态栏已旋转,不再居中

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
//  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
在这里,您应该看到它旋转并居中

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
    theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
有了它们,它可以旋转并全屏显示

您可以下载我的示例代码。

试试这个

 - (void)willEnterFullscreen:(NSNotification*)notification {
     NSLog(@"willEnterFullscreen");

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
  }

 - (void)enteredFullscreen:(NSNotification*)notification {
     NSLog(@"enteredFullscreen");
 }

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];


}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

对我添加到项目中的唯一代码是上面的viewDidLoad,当然还有XIB中的viewForMovie。这是iPad的默认标签栏应用程序。现在它在另一个方向上失败了。我也遇到了同样的问题。还是没有解决办法?我也是。有人知道了吗?你得到答案了吗?