gpt4 book ai didi

iphone - AVAudioPlayer 泄漏?

转载 作者:行者123 更新时间:2023-11-28 18:26:41 25 4
gpt4 key购买 nike

我遇到了一个问题,不知道如何处理。尝试构建带有声音和动画的应用程序。它给了我 1 级和 2 级内存警告。我试过构建和分析,发现了所有这些潜在的漏洞。如果我释放 theAudio,声音将不会播放。有什么提示吗?

这是部分代码和我得到的漏洞

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

最佳答案

Here是迄今为止提出的同一问题的解决方案。

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

为了更好的想法,你可以关注this.

关于iphone - AVAudioPlayer 泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221024/

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