可能重复:
Sound stop playing
iPhone - Sound overlapping with multiple button presses
@interface Cars: UIViewController < AVAudioPlayerDelegate >
{
AVAudioPlayer * theAudio;
}
@property(nonatomic, retain) AVAudioPlayer * theAudio;
(void) playOnce: (NSString * ) aSound;
(void) playLooped: (NSString * ) aSound;
@implementation Cars
@synthesize theAudio;
(void) dealloc {
[theAudio release];
}
(void) playOnce: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 0];
[theAudio setVolume: 1.0];
[theAudio play];
}
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
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];
}
(void) stopAudio {
[theAudio stop];
[theAudio setCurrentTime: 0];
}
(void) audioPlayerDidFinishPlaying: (AVAudioPlayer * ) player successfully: (BOOL) flag {
[player release];
}
如何停止按钮重叠?