可能重复:
Sound stop playing
iPhone - Sound overlapping with multiple button presses
代码给我一个错误“Unused variable'path'”当我按下一个按钮没有声音播放我如何解决这个问题aSound是在h文件中
- (void)playOnce:(NSString *)aSound;
- (IBAction) beatButton50
{
[self playOnce:@"racecars"];
}
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
if([theAudio isPlaying])
{
[theAudio stop];
}
}
- (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];
}