2011-03-03 90 views
1

我一直在試圖弄清楚這一點,並試圖按照任何建議那裏,但我似乎無法'MPMoviePlayerPlaybackDidFinishNotification'在用戶按'完成'在電影播放器​​上。當電影播放器​​上按下'DONE'時,MPMoviePlayerPlaybackDidFinishNotification不起作用

- (void)myMovieFinishedCallback:(NSNotification*)aNotification { 
    MPMoviePlayerController* theMovie=[aNotification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; 
    [theMovie pause]; 
    [theMovie stop]; 
    [theMovie autorelease]; 
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES]; 
} 

- (void)myMovieViewFinishedCallback:(NSNotification*)aNotification { 
    MPMoviePlayerViewController* theMovieView=[aNotification object]; 
    [self dismissMoviePlayerViewControllerAnimated]; 
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovieView]; 
    [theMovieView pause]; 
    [theMovieView stop]; 
    [theMovieView autorelease]; 
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES]; 
} 

- (IBAction)safetyVideo:(id)sender { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Ball_Crunch" ofType:@"m4v"]; 

    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) { 
     MPMoviePlayerViewController*tmpMoviePlayViewController=[[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain]; 
     if (tmpMoviePlayViewController) { 
      [self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController]; 
      tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController]; 

      [tmpMoviePlayViewController.moviePlayer play]; 
     } 

    }else{ 
     MPMoviePlayerController* theMovie = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; 

     [theMovie play]; 
    } 
} 

當'完成'被按下但電話從未被調用時,電影播放正常並消失。有什麼建議麼?

謝謝。

+0

有人在嗎? *蟋蟀* – Entekis 2011-03-04 05:41:41

回答

8

同樣的問題在這裏。發現,當我發送nil作爲對象(而不是MoviePlayerController)回調觸發...

+3

這對我來說也很好,謝謝。 – 2012-08-23 01:36:30

+3

對不起,我沒有 – malhal 2012-10-06 09:24:53

3

我有同樣的問題。 This post救了我。如果視頻全屏顯示,則拍攝MPMoviePlayerDidExitFullscreenNotification而不是MPMoviePlayerPlaybackDidFinishNotification。我會在下面捕獲這兩種情況,以防萬一我稍後改變主意。

- (void)videoButtonClick:(id)sender { 
    MPMoviePlayerController* moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theVideoURL]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; 
    [self.view addSubview:moviePlayerController.view]; 
    moviePlayerController.shouldAutoplay = YES; 
    moviePlayerController.initialPlaybackTime = 0; 
    moviePlayerController.scalingMode = MPMovieScalingModeAspectFit; 
    moviePlayerController.fullscreen = YES; 
    [moviePlayerController play]; 
} 

- (void)moviePlayBackDidFinish:(NSNotification*)notification 
{ 
    MPMoviePlayerController* moviePlayerController = notification.object; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController]; 
    moviePlayerController.initialPlaybackTime = -1; 
    [moviePlayerController stop]; 
    [moviePlayerController release]; 
} 
1

確保爲

moviePlayer.repeatMode = MPMovieRepeatModeNone; 
+0

如果我通過'MPMovieRepeatModeOne'那麼我怎樣才能收到這個通知? – 2015-06-30 12:52:47

+0

我想一次又一次地播放視頻並在視頻開始或結束播放時接收事件.... – 2015-06-30 12:56:44

0

我意識到這是一個老問題,但沒有上述解決我的問題。 如果最終嘗試像我這樣做的一切,請確保您的父視圖控制器不使用相同的方法進行通知。我正在將通知發送到錯誤的地方。

將選擇器名稱更改爲不常用的東西,然後重試。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMovieClosedThisTimeForReal:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
0

this post說你可以觀察到UIWindowDidBecomeVisibleNotificationUIWindowDidBecomeHiddenNotification。即使在iOS 8中也是如此。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(windowVisible:) 
              name:UIWindowDidBecomeVisibleNotification 
              object:self.view.window]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(windowHidden:) 
              name:UIWindowDidBecomeHiddenNotification 
              object:self.view.window]; 

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

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

我也有這個問題,沒有其他解決方案爲我工作。 問題是,當添加觀察者時,對象參數應該是tmpMoviePlayViewController.movi​​ePlayer而不僅僅是moviePlayer。

tmpMoviePlayViewController.movi​​ePlayer(MPMoviePlayerController類)是發送通知的人,而不是tmpMoviePlayViewController(類MPMoviePlayerViewController)。

所以更改此設置:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(myMovieViewFinishedCallback:) 
    name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController]; 

這樣:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(myMovieViewFinishedCallback:) 
    name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController.moviePlayer]; 
相關問題