MPMoviePlayer播放在电影播放器​​上按下“完成”时,DidFinish通知无法正常工作

时间:2011-03-03 20:44:02

标签: xcode

我一直试图解决这个问题,并尝试遵循任何建议,但在用户按下电影播放器​​上的“完成”后,我似乎无法让'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];
     }
}

当按下“完成”但电影从未被调用时,电影播放正常并消失。有什么建议吗?

感谢。

6 个答案:

答案 0 :(得分:8)

这里的问题相同。想通了,当我发送nil作为对象(而不是MoviePlayerController)时,会触发回调......

答案 1 :(得分: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];
}

答案 2 :(得分:1)

确保

    moviePlayer.repeatMode = MPMovieRepeatModeNone;

答案 3 :(得分:0)

我意识到这是一个古老的问题,但以上都没有解决我的问题。 如果您最终尝试像我一样尝试所有内容,请确保您的父视图控制器使用相同的方法进行通知。我把通知发送到了错误的地方。

更改不常用的选择器名称,然后重试。

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

答案 4 :(得分: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");
}

答案 5 :(得分: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];