I'm trying to create a "interactive interview" a soundboard with alot of buttons by using the AVFoundation framework. The soundboard is done but I have one problem, when I press one of my sounds it doesnt stop when I press a new one simultaneously. Is there a easy way of stopping the previous sound if a new one is pressed? My code looks like this
- (IBAction)playSound0 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)playSound1 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"chillwhatup" ofType:@"mp3"];
开发者_运维技巧 AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)playSound2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"gethighorgetlow" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)playSound3 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"icamefromthere" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)playSound4 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"idoeverythinggood" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
I would just have my class keep track of the sound last started and, before I started playing a new sound, stop the previous one, if it is still playing.
AVAudioPlayer has a stop method to stop the sound and a playing method to see if it is playing.
精彩评论