13

我想在视图控制器中显示MPMoviePlayerController,并让用户使用默认控件(如YouTube应用)切换全屏。我使用的裸机例如下面的代码:MPMoviePlayerController在iPad中的全屏怪癖

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

这种运作良好,直到用户进行视频全屏播放,旋转设备,并点击屏幕上。状态栏显示在错误的位置,如下面的屏幕截图所示。

screenshot

我与模板iPad的标签栏应用程序的工作。我只在XIB中添加了viewDidLoad,视图变量和一个UIView来显示电影播放器​​。

我在做什么错?

+0

我遇到了同样的问题。仍然没有解决方法? – samvermette 2010-06-09 18:03:35

+0

我也是。任何人都有这个工作? – V1ru8 2010-08-18 14:40:01

+0

你有答案吗? – Jingwei 2013-06-28 00:59:44

回答

2

IsAutorotateToInterfaceOrientation:interfaceOrientation对所有支持的方向返回YES?

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

如果你提供了更多的代码,它会有所帮助。

+0

是的。我已经添加到项目中的唯一代码是上面的viewDidLoad,当然还有XIB中的viewForMovie。这是iPad的默认选项卡栏应用程序。 – hpique 2010-05-19 15:30:08

1

您是否正在为您的UI使用界面构建器?如果确实如此,请确保在视图属性检查器中将视图的方向设置为“横向”。

+0

它现在在其他方向失败。 – hpique 2010-05-20 15:40:12

1

有同样的问题,只是花了半天整理出来。使用纵向的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]; 
    } 
0

您是否解决了这个问题?

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

此代码可能会对您有所帮助。

3

是的,我也遇到过这个问题。它绝对是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]; 
0

发现了它。

有同样的问题 - 这里是我做的。我会建议将代码逐个添加到您的项目中,以查看它的工作原理。

首先 - 我放东西的是肖像模式。

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

然后我把电影推到状态栏上。注意 - 这是假设视频有一个4×3宽高比

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))]; 

然后,在应用程序启动时的地方(在我的项目,它是在didFinishLaunchingWithOptions功能) - 反正,你只需要访问窗口对象。

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

然后电影和状态栏第一2

这里你应该看的电影和状态栏旋转,不再集中

// 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); 

并与他们一起,它旋转和全屏

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

0

试试这个

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