我有个问题,不知道该怎么处理。Tyring来构建一个具有声音和动画的应用程序。它会给我一个级别1,然后是级别2内存警告。我试着构建和分析,我得到了所有这些潜在的泄漏。如果我释放theAudio,声音就不会播放。有什么提示吗?
这是我得到的代码的一部分和leakes
- (IBAction)playsound2
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
*- **Method returns an Objective-C object with a +1 retain count (owning reference)***
theAudio.delegate = self;
[theAudio play];
***- Object allocated on line 302 and stored into 'theAudio' is no longer referenced after this point and has a retain count of +1 (object leaked)***
cat1.hidden = 0;
cat.hidden = 1;
[cat1 startAnimating];
cat.center = cat1.center;
[self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0];
catLabel.hidden = 0;
}发布于 2011-08-28 20:58:38
到目前为止,对于同样的问题,是解决方案。
- (AVAudioPlayer *)audioPlayerWithContentsOfFile:(NSString *)path {
NSData *audioData = [NSData dataWithContentsOfFile:path];
AVAudioPlayer *player = [AVAudioPlayer alloc];
if([player initWithData:audioData error:NULL]) {
[player autorelease];
} else {
[player release];
player = nil;
}
return player;
}为了获得更好的想法,你可以关注
发布于 2012-05-10 09:44:59
您必须为AVAudioPlayer类实例声明一个实例变量。
//Instance variable:
{
AVAudioPlayer* theAudio;
}
- (IBAction)playsound2
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp3"];
if (!theAudio ) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
if (theAudio ) {
theAudio.delegate = self;
[theAudio play];
}
}
cat1.hidden = 0;
cat.hidden = 1;
[cat1 startAnimating];
cat.center = cat1.center;
[self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0];
catLabel.hidden = 0;
}https://stackoverflow.com/questions/7221024
复制相似问题