0

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

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

4

2 回答 2

0

因为,我没有单独的视图控制器类,当我使用导航控制器代表设置电影播放器​​的通知时,它起作用了

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

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

}

于 2013-09-10T11:21:41.723 回答
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];
}
于 2013-09-10T11:11:21.233 回答