当我的电影完成播放时,应用程序崩溃了

时间:2011-03-16 18:37:07

标签: objective-c ipad mpmovieplayercontroller

我尝试通过点击按钮启动电影。当我点击电影播放器​​出现并正常工作。电影播放到最后,电影播放器​​消失。它消失后,我的应用程序崩溃......

我使用以下代码在Tab Bar应用程序中使用我的View Controller:

- (void)moviePlayBackDidFinish:(NSNotification *) aNotification{

    MPMoviePlayerController *player = [aNotification object];
    [player setFullscreen:NO animated:YES];
    [player.view removeFromSuperview];
    [player stop];
    player.initialPlaybackTime = -1.0;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [player release];
    player=nil;
}

- (IBAction)playVideo:(UIButton *)playButton{
    NSString *url = [[NSBundle mainBundle] pathForResource:@"Teaser 04" ofType:@"mov"];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    player.shouldAutoplay = YES;
    player.view.frame = CGRectMake(0., 44., self.view.bounds.size.width, self.view.bounds.size.height-44);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [self.view addSubview:player.view];
    [player play];
}

我不知道我做错了什么。我只想配置一个启动视频的按钮,当视频播放完毕后,MoviePlayer视图会消失,应用程序会回到我的初始.xib

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

我找到了解决方案,我不知道它是否是最好的,但我想要它的工作:

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

}

- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:@"Teaser 04" ofType:@"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(0, 0, 950, 600);
[self.view addSubview:player.view];
[player play];

}

答案 1 :(得分:0)

我可能错了,但我的猜测是玩家在backtrace的某个地方被用来调用moviePlayBackDidFinish:...如果我是对的,那么将对象解除分配 - 看起来你正在做 - 这是个坏消息,因为在堆栈的某个地方仍然需要该对象。

查看此帖子了解更多详情:How to release MPMoviePlayerController?