【问题标题】:EXC_BAD_ACCESS when movie played the second time电影第二次播放时的 EXC_BAD_ACCESS
【发布时间】:2013-10-21 06:16:23
【问题描述】:

我正在使用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];
}

请帮忙。

【问题讨论】:

  • 查看违规代码行设置异常断点developer.apple.com/library/ios/recipes/…
  • 异常断点也指向同一行。
  • 在方案中启用僵尸对象
  • 释放后尝试设置moviePlayer = nil,以防止潜在的悬空指针
  • @LearnCocos2D:很抱歉没有回复,我把这个问题留了一段时间,但我仍然无法解决它。启用僵尸方案后,我在日志[Instructions handleTap:]: message sent to deallocated instance 中得到以下信息。我还设置了moviePlayer = nil,但这并没有改变任何东西。

标签: objective-c cocos2d-iphone mpmovieplayercontroller mpmovieplayer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 2011-01-03
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多