【问题标题】:MPMoviePlayerPlaybackDidFinishNotification not working when 'DONE' is pressed on movie player在电影播放器​​上按下“完成”时,MPMoviePlayerPlaybackDidFinishNotification 不起作用
【发布时间】:2011-07-08 08:44:20
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 有人在吗? * 蟋蟀 *

标签: xcode


【解决方案1】:

我也遇到了这个问题,没有其他解决方案适合我。 问题是添加观察者的时候,object参数应该是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];

【讨论】:

    【解决方案2】:

    您可以观察UIWindowDidBecomeVisibleNotificationUIWindowDidBecomeHiddenNotification,如this post 中所述。这甚至适用于 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");
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        确保

            moviePlayer.repeatMode = MPMovieRepeatModeNone;
        

        【讨论】:

        • 如果我通过MPMovieRepeatModeOne 那么我如何才能收到此通知??
        • 我想一遍又一遍地播放视频并在视频开始或结束播放时接收事件......
        【解决方案5】:

        我遇到了同样的问题。 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];
        }
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-01-05
            • 1970-01-01
            • 1970-01-01
            • 2023-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多