电影第二次播放时的EXC_BAD_ACCESS

时间:2013-10-21 06:16:24

标签: objective-c cocos2d-iphone mpmovieplayercontroller mpmovieplayer

我正在使用mpmovieplayercontroller在我的游戏中播放电影。当用户点击屏幕时,我运行一个方法,用游戏主菜单替换包含电影的当前图层。它第一次工作正常,但是当我第二次尝试从主菜单播放视频时,我在行上出现了EXC_BAD_ACCESS错误

int retVal = UIApplicationMain(argc, argv, nil, @"AppController");

在main.m。

请在下面找到相关代码。

-(void)playVideo    {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.repeatMode = MPMovieRepeatModeOne;

// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    // Use the new 3.2 style API
    moviePlayer.controlStyle = MPMovieControlStyleNone;
    moviePlayer.shouldAutoplay = YES;
    // This does blows up in cocos2d, so we'll resize manually
    [moviePlayer setFullscreen:YES animated:YES];

    CGSize winSize = [[CCDirector sharedDirector] winSize];       
    moviePlayer.view.frame = CGRectMake(0, 0, winSize.width, winSize.height);    //width and height are swapped after rotation        
    [[[CCDirector sharedDirector] view] addSubview:moviePlayer.view ];

    [moviePlayer play];

    UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapGestureRecognizer.delegate = (id)self;
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [[[CCDirector sharedDirector] view] addGestureRecognizer:tapGestureRecognizer];

    [tapGestureRecognizer release];

} else {
    // Use the old 2.0 style API
    moviePlayer.controlStyle = MPMovieControlStyleNone;
    [moviePlayer play];
}

}

- (void)handleTap:(UITapGestureRecognizer *)gesture {

[moviePlayer stop];
[[MenuManager sharedMenuManager] runMenu:kMMenuLayer];

}

// this enables you to handle multiple recognizers on single view
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

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

// If the moviePlayer.view was added to the openGL view, it needs to be removed
if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    [moviePlayer.view removeFromSuperview];
    CCLOG(@"this block is okay");
}

[moviePlayer release];
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

您收到邮件的原因是发送到解除分配的实例是因为您没有从视图中删除tapGestureRecognizer。我相信你的MenuManager单例会将当前图层替换为另一个图层,但由于tapGestureRecognizer仍然是视图的一部分,因此它将尝试访问handleTap,此时会取消分配。在handleTap中的单例调用之前添加以下行。

 [[[CCDirector sharedDirector] view] removeGestureRecognizer:tapGestureRecognizer];