Here's a quick way to do it (using Swift 2.0 FYI). Define a counter as a global variable and set it to 0. When you press the button again, stop the audio and reset its start time. Hope this helps.
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer {
let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
let url = NSURL.fileURLWithPath(path!)
var audioPlayer:AVAudioPlayer?
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("NO AUDIO PLAYER")
}
return audioPlayer!
}
@IBAction func buttonTap(sender: AnyObject) {
if (counter%2==0)
{
backMusic = setupAudioPlayerWithFile("Etudes", type: "mp3")
backMusic.play()
}
else
{
backMusic.stop()
backMusic.currentTime = 0.0
}
counter++