AVAudioPlayer 泄漏并发出警告,最后我的应用程序崩溃了
我正在使用 AVAudioPlayer,我再次播放声音并再次在我的应用程序中调用此方法 100 次。问题是我总是 alooc 它,但当我要释放时,声音不起作用。 我该怎么办。
-(void) ButtonSound
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player play];
[fileURL release];
}
内存泄漏并且应用程序崩溃,我想立即分配播放器并一次又一次地使用它。
I am using AVAudioPlayer and i am plying sound again and agian and call this method 100 times in my application.The problem is that i always alooc it but when i am going to release, sound does not work.
what will i do.
-(void) ButtonSound
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player play];
[fileURL release];
}
memory is leak and the application is crashed, I want to allocate player at once and use it again and again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行类似的操作
您可以在头文件中
,然后在实现中声明播放器
You can do it something like this
on you header file, declare the player already
then on the implementation
将代码更改为:
您还需要在
dealloc
方法中释放player
。您可能还需要检查单击按钮时player
是否已经在播放,如果是,则可能跳过此方法。您可能还需要
保留
您的播放器对象,但这取决于您声明它的方式(示例中未显示)。Change your code to this:
You also need to release
player
in yourdealloc
method. You also probably need to check whetherplayer
is already playing when you click the button, and perhaps skip this method if so.You may also need to
retain
your player object, but that depends on how you've declared it (not shown in your sample).这段代码没有错误,我使用这段代码没有发现任何泄漏
This code is error free and i have not found any leaks by using this code