MPMoviePlayerPlaybackDidFinishNotification is not calling back

1.7k views Asked by At

I have a scenario where the notifications for MPMoviewPlayerController are not being called.

Please check the below code and let me know the issue:

- (void)playVideo:(nsstring *)filePath onController:(UIViewController *)controller {

self.mediaPlayerContainerViewController = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.mediaPlayerContainerViewController];
NSURL *videoUrl = [NSURL fileURLWithPath:filePath];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
self.playerController = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
self.playerController.view.frame = self.mediaPlayerContainerViewController.view.bounds;
[self.playerController setFullscreen:NO animated:NO];
[self.playerController setControlStyle:MPMovieControlStyleEmbedded];
[self.mediaPlayerContainerViewController.view addSubview:self.playerController.view];
[self.playerController prepareToPlay];
self.playerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.mediaPlayerContainerViewController.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[controller presentViewController:navController animated:YES completion:^(void) {
    self.mediaPlayerContainerViewController.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:
                                                    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Homebutton.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(dismissMediaPlayerViewController)],
                                                    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ArrowStandardCircle.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(dismissMediaPlayerViewController)],
                                                    nil];
    [self.playerController play];
}];
}

- (void)movieFinishedCallBack:(NSNotification *) aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [player stop];
}

I am not sure where I set the notifications wrong.

2

There are 2 answers

0
karthika On
-(void)playVideo
{

    NSURL *url = [NSURL URLWithString:@".mp4"];
    self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    NSError *_error = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
    [self presentMoviePlayerViewControllerAnimated:self.moviePlayer];

}
-(void)playbackFinishedCallback:(NSNotification *)notification{

    self.moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
    self.moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];
    [self.moviePlayer.view removeFromSuperview];
}
0
Vijay On

As, I have no seperare viewcontroller class, it worked when i used the navigationcontroller delegates to set the notifications for movie player

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

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

}