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