iphone - AVAudioPlayer泄漏和警告,最后我的应用程序崩溃了

标签 iphone memory-leaks audio avaudioplayer

我正在使用AVAudioPlayer并再次使用agian声音,并在我的应用程序中调用此方法100次。问题是我总是无法使用它,但是当我要释放时,声音不起作用。
我该怎么办。

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

}

内存泄漏并且应用程序崩溃了,我想立即分配播放器并再次使用它。

最佳答案

你可以这样做

在头文件上,已经声明播放器

AVAudioPlayer *buttonSoundPlayer;

然后执行
-(void)playButtonSound {
    if(buttonSoundPlayer == nil) {
       NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
       buttonSoundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
       [fileURL release];
    }
    [buttonSoundPlayer play];
}

关于iphone - AVAudioPlayer泄漏和警告,最后我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5887161/

相关文章:

ios - 通过外观代理设置 UIBarButtonItem 的文本颜色

ios - 为什么我在 [anObject release] 时没有内存泄漏,而我在 self.anObject = nil 时遇到问题?

c - C中的动态数组push()导致Valgrind错误

audio - 如何使用soundio一次播放多个流?

HTML5 视频 - 仅从视频中获取配乐

iphone - 由于事件存储更改而导致应用程序崩溃?

javascript - 在javascript中的iphone/ipad上强制停止动量滚动

iphone - 在表格 View 中选择新行时如何取消选择选定的行?

python - 即使我在每次迭代结束时删除所有变量,python for 循环中也会出现内存泄漏

audio - OpenAL-Soft 对源数量有上限吗?