我正在使用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/