2010-07-13 95 views
22

我有很多在我的iPad應用程序顯示一個全屏電影麻煩,然後允許用戶無論使用哪種完成按鈕或「非全屏」予以駁回播放器控制按鈕。正確顯示和iOS 3.2(iPad版)駁回全屏的MPMoviePlayerController

起初,我使用MPMoviePlayerViewController爲電影的介紹,但我並沒有從MPMoviePlayerController對象接收的進入/退出全屏通知,所以我切換到做我自己。

我可以讓電影全屏顯示(雖然過渡很快),但當按下「完成」或「非全屏」按鈕時,播放器不採取任何操作。我已爲我下面的代碼:

- (void)startPlayingMovieWithURLString:(NSString *)movieURLString { 
    // I get all of these callbacks **EXCEPT** the "willExitFullScreen:" callback. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    [self.moviePlayerController setContentURL:someExistingURL]; 

     // "self" is a UIViewController subclass, and is presented as a "fullscreen" modal view controller from its parent 
     // I'm setting the movie player's view's frame to take up the full rectangle of my view controller, but really I want the movie to be completely removed when the user presses "done" (that is, removed from the view hierarchy). Not sure when/where to do this. 
    self.moviePlayerController.view.frame = self.view.frame; 
    [self.view addSubview:self.moviePlayerController.view]; 
    [self.moviePlayerController setFullscreen:YES animated:YES]; 

} 

這裏是我的didFinish回調

- (void)didFinishPlayback:(NSNotification *)notification { 
     // This ends up recursively telling the player that playback ended, thus calling this method, thus…well you get the picture. 
     // What I'm trying to do here is just make the player go away and show my old UI again. 
    [self.moviePlayerController setFullscreen:NO animated:YES]; 
} 

所以,很顯然,我做錯了什麼,但我已經起來的文檔,我下來的代碼無法弄清楚如何讓電影消失。我認爲這會比這更直觀。我究竟做錯了什麼?

回答

66

這裏是如何發生的事件 - >通知的工作:

  • 用戶按下 '完成' 按鈕

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
  • 用戶按下 '離開全屏' 運輸按鈕

    • MPMoviePlayerWillExitFullscreenNotification
    • MPMoviePlayerDidExitFullscreenNotification
    • 注意播放不會停止
  • 電影達到最終

    • MPMoviePlayerPlaybackDidFinishNotificationMPMoviePlayerPlaybackDidFinishReasonUserInfoKey設置爲MPMovieFinishReasonPlaybackEnded
    • 如果你打電話給你的MoviePlayerController實例setFullscreen:NO animated:YES從這個通知時,會再拿到WillExitDidExit通知。
    • 請注意,當用戶按下「完成」或「離開全屏」按鈕時,您不會收到PlaybackDidFinish通知。

所以,通常情況下,如果你想擺脫MoviePlayer的看法,你需要把[self.moviePlayerController.view removeFromSuperview]DidExitFullscreen通知處理程序。 WillExitFullscreen太早。

這裏是我的代碼:

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

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

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

- (void)exitedFullscreen:(NSNotification*)notification { 
    NSLog(@"exitedFullscreen"); 
    [self.movieController.view removeFromSuperview]; 
    self.movieController = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)playbackFinished:(NSNotification*)notification { 
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
    switch ([reason intValue]) { 
     case MPMovieFinishReasonPlaybackEnded: 
      NSLog(@"playbackFinished. Reason: Playback Ended");   
       break; 
     case MPMovieFinishReasonPlaybackError: 
      NSLog(@"playbackFinished. Reason: Playback Error"); 
       break; 
     case MPMovieFinishReasonUserExited: 
      NSLog(@"playbackFinished. Reason: User Exited"); 
       break; 
     default: 
      break; 
    } 
    [self.movieController setFullscreen:NO animated:YES]; 
} 

- (void)showMovie { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    NSURL* movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tron" ofType:@"mov"]]; 
    self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    self.movieController.view.frame = self.view.frame; 
    [self.view addSubview:movieController.view]; 
    [self.movieController setFullscreen:YES animated:YES]; 
    [self.movieController play]; 
} 
+1

這是有幫助的,我現在可以在用戶按下完成時退出全屏,但-playbackFinished:回調從來沒有爲我執行過,我不能爲我的生活找出原因。你知道這有可能發生的原因嗎? – jbrennan 2010-07-15 02:43:46

+0

即使電影玩到最後?奇怪的是,我從來沒有遇到過這種情況。 – 2010-07-15 03:49:53

+3

如果在觸摸「完成」和全屏退出時調用MPMoviePlayerDidExitFullscreenNotification,您怎麼知道哪個被調用?也許用戶只能退出全屏,在這種情況下,你不想刪除我猜測的視圖。 – 2011-06-10 13:07:44

0

是。那很棒。真的有上面提到的通知...

但是,沒有MPMoviePlayerPlaybackWillFinishNotification somewhy !!! 這確實是一個問題。

當你調用電影播放器​​模式(無論其使用以下方法presentViewController/presentModalViewController/presentVideoController),如果定義.fullScreen = YES,它預計不會叫MPMoviePlayerWillExitFullscreenNotification通知所有(顯然,因爲我們不是從全屏進入/退出,而只是呈現/解散控制器)。

但是確實沒有任何視頻即將完成和關閉的通知。這是需要的(除了可能的其他情況),以趕上解僱過渡開始的時刻。 (當然,轉換在調用MPMoviePlayerPlaybackDidFinishNotification之前開始)。同時,應用程序:supportedInterfaceOrientationsForWindow:對於先前顯示的控制器在通知之前調用,並且沒有辦法說AppDelegate我們當前的控制器必須以另一個方向顯示。因爲我的視頻是全屏,也沒有顯示任何控制(這是一種介紹,所以我只是直到它完成)我的解決方案是隻是有一個計時器,它會檢查每個短嘀嗒聲(0.1秒)什麼是視頻當前位置 ...它接近尾聲,那麼這是我自己通知的時刻。

相關問題