AVAudioPlayer泄漏?

编程入门 行业动态 更新时间:2024-10-28 14:26:57
本文介绍了AVAudioPlayer泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I've got a problem and don't know how to handle it. Tyring to build an app with sound and animation. It gives me a level 1 and then level 2 memory warning. I've tried build and analyze and i got all these potential leaks. If I release theAudio the sound won't play. Any hints?

这是代码的一部分,也是我所泄漏的内容

Here is a part of the code and the leakes I've got

- (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 is the solution for the same problem asked so far.

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

为了获得更好的主意,您也可以遵循 此.

And for better idea you can follow this.

更多推荐

AVAudioPlayer泄漏?

本文发布于:2023-11-26 16:03:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AVAudioPlayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!