How to Create a button to stop the music in xcode

##This code has a button when pressed it plays the music. However there is no way to stop the music. I would like to create a code that if the button is pressed a even number of times the song plays if the button is pressed a odd number of times the song stops. Even numbers are 0, 2,4,6 etc and Odd numbers are 1,3,5,7 etc. I just want to use one button if possible. Thanks

import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func PlaySound(sender: AnyObject) {
    // Set the sound file name & extension
    let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("x", ofType: "mp3")!)
    
    do {
        // Preperation
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }
    
    // Play the sound
    do {
        audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
    } catch _{
    }
    
    audioPlayer.prepareToPlay()
    audioPlayer.play()
    
}

}

OK, here’s what you need to do. Create a standard button with touch up inside action. When the first action is recorded, start a timer for a short time like 0.5 seconds, this is the time that a user has to tap before the action is considered complete. Whenever the button is tapped a counter increments, counting the total number of taps. When the timer fires the number of counted taps is processed (%2) to detect odd or even and the counter is reset to zero.

Make sure your button is standard button. and create button action is as follow :

@IBAction func PlaySound(sender: UIButton) {
    
    if sender.selected == false {
        
        sender.selected = true
        
        // Here you have to maintain that music have to start from where you left.
        // Set the sound file name & extension
        let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("x", ofType: "mp3")!)
        
        // Preperation
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch _ { }
        
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ { }
        
        // Play the sound
        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
        } catch _{ }
        
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
    else {
        
        sender.selected = false
        
        audioPlayer.stop()
    }
}