Farzam
Farzam

Reputation: 177

Button Tapped Frequently

I'm trying to make a button play a sound which is about 1 second.I Changed the event to "Touch Down" so when the button is pressed, it starts playing the sound. The problem is that if I press the button during that 1 second playing, nothing happens and i have to wait until the sound is over. but I want the button to ignore the current audio and start playing it again.(Kinda like a video game with a bang shot when the user presses the shoot button twice). How can i achieve this in swift?

here's the code for the button :

@IBAction func secondButton(sender: UIButton) {

    brain.soundPlayer.play()

} // brain.soundplayer records anaudio and the button it as the sound.

Upvotes: 0

Views: 60

Answers (2)

Braelyn B
Braelyn B

Reputation: 144

You can stop and restart the sound when the button is pushed.

@IBAction func secondButton(sender: UIButton) {
    brain.soundPlayer.stop()
    brain.soundPlayer.play() 
}

Upvotes: 0

Caleb
Caleb

Reputation: 5626

You can set the currentTime of your AVAudioPlayer to 0. Then use play.

audioPlayer.currentTime = 0
audioPlayer.play()

Upvotes: 1

Related Questions