Search code examples
iosswiftaudiouibuttonrecorder

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.

Solution

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

    audioPlayer.currentTime = 0
    audioPlayer.play()