有关Objective C的帮助?我的应用程序一直在崩溃?

时间:2011-03-18 11:20:29

标签: objective-c crash avaudioplayer

我正在为Iphone / Itouch创建一个应用程序,但我继续遇到几个主要的泄漏,只是让应用程序崩溃。在我的游戏中,当我按下播放时(从主屏幕)它进入另一个有游戏的页面。但是,在它出现之后,在控制台中我得到一个警告:内存级别= 1。可能会发生什么?这是我的ViewDidLoad方法:

-(void)viewDidLoad {

[super viewDidLoad];
array = [[NSMutableArray alloc] initWithCapacity:100];
bulletArray = [[NSMutableArray alloc] initWithCapacity:100];
pos = CGPointMake(0.0,-5.0);


NSString *path = [[NSBundle mainBundle] pathForResource:@"GloriousMorning" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];

}

另外,第二个问题,因为我的游戏是射击游戏,用户按下标题为“火”的按钮。但是,每次我在设备上测试我的应用程序时,当我按下开火按钮时它会崩溃。这是我的消防按钮的代码。

-(IBAction)Fire {   
NSString *path = [[NSBundle mainBundle] pathForResource:@"gunShot" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];


//IBOutlet UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
newBullet.frame = CGRectMake(0, 0, 10.0, 10.0);
newBullet.center = CGPointMake(239.0, 236.0);
[bulletArray addObject:newBullet];
[self.view addSubview:newBullet];

}

首先,我创造一个声音。然后,我将一颗子弹放在枪当前所在的位置,然后将它添加到一个阵列中,以便每隔.01秒,在另一段代码中,我可以穿过阵列并检查每个子弹以检测碰撞。 / p>

请告诉我我做错了什么。感谢!!!

单击导致应用程序崩溃的Fire按钮时的错误是:

GDB:数据格式化程序暂时不可用,将在“继续”后重试(加载共享库时出现未知错误“

而且当我尝试播放音频时,我想我正在制造巨大的泄漏,至少这是有人告诉我的。 (如果是这种情况,请告诉我如何解决它)

1 个答案:

答案 0 :(得分:0)

您已分配了音频,因此您需要在完成后释放它。可能更好地制作一个ivar,这样你每次按下开火按钮时都不必设置和拆除音频。