iOS - 检测影片已完成

时间:2013-09-02 19:35:37

标签: ios cocoa-touch mpmovieplayercontroller

我正在使用MPMoviePlayerViewController来播放电影,我创建了一个应该检测电影何时完成的方法,然后运行一个方法:

- (void)movieFinishedWithSelector:(SEL)selectors {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(selectors)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:[player moviePlayer]];
  }

并像这样使用此方法,但不起作用。

[self movieFinishedWithSelector:@selector(finished)];

我错过了什么吗?

3 个答案:

答案 0 :(得分:2)

selectors参数已经是一个选择器。不要使用@selector

- (void)movieFinishedWithSelector:(SEL)selector {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:selector
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:[player moviePlayer]];
}

答案 1 :(得分:2)

加载电影时创建通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(myMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:movieController];

完成后,myMovies Finished将被称为

-(void)myMovieFinished:(NSNotification *)aNotification
{
    NSLog(@"%@",aNotification.userInfo);
   int reason =    [[[aNotification userInfo]valueForKey:MPMoviePlayerPlaybackDidFinishNotification]intValue];
    if (reason==MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Movie finished playing");
    }
    else if (reason==MPMovieFinishReasonUserExited)
    {
        NSLog(@"Movie finished because user exited");
    }
    else if (reason==MPMovieFinishReasonPlaybackError)
    {
        NSLog(@"movie finished playback error");
    }

    movieController=[aNotification object];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movieController ];

}

答案 2 :(得分:0)

你是如何定义选择器的?它应该是:

- (void)movieDidFinish:(NSNotification*)notification