Hi guys, new to these forums and ios development.
I am trying to create a simple app which has a button which when touched will play a random sound from a series. When I click the button, it does play the sound, but when touched again it will play the second sound over the first. How can I make the previous song stop when the button is touched again?
Thanks in advance- below is the code.
Code:
- (IBAction)randomsound {
NSString *title = nil;
NSString *path = nil;
AVAudioPlayer *audio = nil;
int Number = arc4random() % 2;
switch (Number) {
case 0:
path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audio stop];
[audio play];
break;
case 1:
path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audio stop];
[audio play];
break;
break;
default:
break;
}
}