强制触发moviePlaybackComplete方法

时间:2011-08-11 06:17:15

标签: objective-c cocoa-touch ipad

抱歉问题标题。不知道怎么称呼它。 继承我的问题。我正在iPad应用中加载视频。它是在一种灯箱中打开的。 视频播放正确。 现在,当我触摸背景时,视频应该关闭并释放,就像视频结束时一样(moviePlaybackComplete) 我不知道该怎么做。到目前为止,我的代码是:

    - (IBAction)playVideo:(id)sender
{
    UIButton *playButton = (UIButton *) sender; 

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"mymovie" ofType:@"mp4"];
    NSURL    *fileURL  = [NSURL fileURLWithPath:filepath];
    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

    [moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x,
                                                    playButton.frame.origin.y,
                                                    640,
                                                    360)];

    [self.view addSubview:moviePlayerController.view];

    [moviePlayerController play];
}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}

- (IBAction)hideVideo:(id)sender
{
    if(fadeView.hidden == NO){
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(finishedVideoFading)];
        fadeView.alpha = 0;
        [UIView commitAnimations];  

        [moviePlayerController stop];
        [moviePlayerController.view removeFromSuperview];
        [moviePlayerController release];
    }
}

1 个答案:

答案 0 :(得分:0)

那个“灯箱”是一个视图控制器吗?如果是,那么当视频结束时关闭视图。

[[self navigationController] popViewControllerAnimated: YES];