Reputation: 3
I am making a noiseMaker application for iOS in xCode where there are different buttons that play different sounds. Each sound is given a tag of 0-3 along with each button in the storyboard. And the randomize button has a tag of 4, Here is the code so far:
func play (index: Int) {
if !player.isEmpty && index >= 0 && index < player.count {
player[index].play()
}
if index == 4{
let randomNumber = Int(arc4random_uniform (4) + 0)
player[randomNumber].play()
}
However, what I want is for the current song to stop playing when I press on another button or even on the same button again. I believe the stop AVAudioPlayer code would go right when the function "play" begins so that it stops the player before any other song starts playing.
All help is appreciated. Thanks in advance...
Upvotes: 0
Views: 1040
Reputation: 23882
You can check that currently player is playing or not.
Check the playing
property returns status of player is playing or not.
if (aPlayer.playing) {
aPlayer.stop()
//Then play again by aPlayer.play()
}
else {
aPlayer.play()
}
Upvotes: 1