gpt4 book ai didi

iphone ios xcode 4.2 - EXC_BAD_ACCESS 信号

转载 作者:行者123 更新时间:2023-11-29 04:46:46 26 4
gpt4 key购买 nike

我正在设计一个 iPhone 应用程序,它将在本地播放视频。当我单击模拟器中的按钮时,它播放得很好,但是当它停止或当我手动结束它时,它崩溃并不断给我带来这个问题。我尝试清理、构建、分析并再次运行,但仍然相同。有什么帮助吗?

我的代码是:

MoviePlayerViewController.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MoviePlayerViewController : UIViewController {

}
-(IBAction)playMovie:(id)sender;
@end

以及MoviePlayerViewController.m中的主要部分

- (IBAction)playMovie:(id)sender {
NSString *movpath = [[NSBundle mainBundle] pathForResource:@"think" ofType:@"mp4"];
MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

[self.view addSubview:mpviewController.view];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
[mp prepareToPlay];
mp.scalingMode=MPMovieScalingModeAspectFill;
[[mpviewController moviePlayer] play];
}

- (void)playbackFinishedCallback:(NSNotification *)notification {
MPMoviePlayerViewController *mpviewController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];
[mpviewController.view removeFromSuperview];
[mpviewController release];
}

最佳答案

代码中存在一些问题,以下是修复:

1> 删除 [mpviewController release]; 因为它是使用返回 *autorelease* 对象的方法创建的。([notification object])。要释放mpviewController对象,请将其声明为实例变量并释放它并使其为零。

if(mpviewController != nil) 
{
[mpviewController release];
mpviewController = nil;
}

2> 由于您已将 mpviewController 声明为实例变量,因此无需通过 [notification object] 访问 mpviewController 变量,因为它的不存在,因为当您将观察者添加到通知中心时尚未提供它。

3> 替换以下代码行:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];

 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

说明:当您添加观察者时,您没有提供任何对象信息,但在删除时您会提供任何对象信息

所以现在你的代码将变成:

- (void)playbackFinishedCallback:(NSNotification *)notification {

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mpviewController.view removeFromSuperview];
if(mpviewController != nil)
{
[mpviewController release];
mpviewController = nil;
}
}

此外,在此 Controller 的 - (void) dealloc 中,您应该编写类似的代码来释放 mpviewController

谢谢,

关于iphone ios xcode 4.2 - EXC_BAD_ACCESS 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com