可能重复:
Sound stop playing
iPhone - Sound overlapping with multiple button presses
我的代码有声音按钮,当按下它时会播放声音,但是当按下另一个声音时,声音会重叠。我已经尝试了一些东西,但没有任何帮助。如何停止重叠播放的声音?
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
if([theAudio isPlaying])
{
[theAudio stop];
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}