MPMoviePlayerPlaybackDidFinishNotification未回拨

时间:2013-09-10 11:03:23

标签: ios objective-c nsnotificationcenter mpmoviewcontroller

我有一个不会调用MPMoviewPlayerController通知的情况。

请检查以下代码并告知我们问题:

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

我不确定我在哪里设置通知错误。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

因为,我没有seperare viewcontroller类,当我使用navigationcontroller委托设置电影播放器​​的通知时它工作

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

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

}