1

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

请帮忙。

4

1 回答 1

0

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

 [[[CCDirector sharedDirector] view] removeGestureRecognizer:tapGestureRecognizer];
于 2013-12-03T03:38:19.213 回答