MPMoviePlayerPlaybackDidFinishNotification не работает, когда на проигрывателе фильмов нажата кнопка «ГОТОВО» - PullRequest
1 голос
/ 03 марта 2011

Я пытался понять это и пытался следовать любым советам, но я не могу заставить 'MPMoviePlayerPlaybackDidFinishNotification' работать после того, как пользователь нажмет 'Done' на проигрывателе фильмов.

- (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 ]

8 голосов
/ 28 апреля 2011

Та же проблема здесь.Выяснилось, когда я отправляю nil как объект (вместо MoviePlayerController) обратный вызов срабатывает ...

3 голосов
/ 09 июня 2011

У меня была такая же проблема. Этот пост спас меня.Если видео переходит в полноэкранный режим, снимите 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 голос
/ 09 сентября 2014

Убедитесь, что

    moviePlayer.repeatMode = MPMovieRepeatModeNone;
0 голосов
/ 12 мая 2015

У меня тоже была эта проблема, и никакое другое решение не помогло мне.Проблема заключается в том, что при добавлении наблюдателя параметром объекта должен быть tmpMoviePlayViewController.moviePlayer, а не просто moviePlayer.

tmpMoviePlayViewController.moviePlayer (класс MPMoviePlayerController) является тем, который отправляет уведомление, а не класс tmpontrollerPMViewMerPlayerMViewPlayerMViewPlayer ()..

Поэтому измените это:

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

на это:

[[NSNotificationCenter defaultCenter] addObserver:self
     selector:@selector(myMovieViewFinishedCallback:) 
     name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController.moviePlayer];
0 голосов
/ 06 октября 2014

Вы можете наблюдать до UIWindowDidBecomeVisibleNotification и UIWindowDidBecomeHiddenNotification, как указано в этого поста . Это работает даже в 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 голосов
/ 16 сентября 2014

Я понимаю, что это старый вопрос, но ни один из вышеперечисленных не решил мои проблемы.Если в итоге вы попробуете все, как я, убедитесь, что ваш родительский контроллер представления не использует тот же метод для уведомления.Я отправлял уведомление в неправильное место.

Измените имя селектора на что-то менее обычное и повторите попытку.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMovieClosedThisTimeForReal:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...