# How to Create a button to stop the music in xcode

**URL:** <https://forums.kodeco.com/t/how-to-create-a-button-to-stop-the-music-in-xcode/18509>\
**Category:** Swift\
**Created:** [August 16, 2016, 3:24am UTC](https://forums.kodeco.com/t/how-to-create-a-button-to-stop-the-music-in-xcode/18509 "2016-08-16T03:24:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [August 16, 2016, 3:24am UTC](https://forums.kodeco.com/t/how-to-create-a-button-to-stop-the-music-in-xcode/18509/1 "2016-08-16T03:24:24Z")

</div>

##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()
    
}

```

}

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [August 16, 2016, 8:48pm UTC](https://forums.kodeco.com/t/how-to-create-a-button-to-stop-the-music-in-xcode/18509/2 "2016-08-16T20:48:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vimipatel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vimipatel/32/83106_2.png) [@vimipatel](https://forums.kodeco.com/u/vimipatel)\
**Post date:** [September 23, 2016, 5:04am UTC](https://forums.kodeco.com/t/how-to-create-a-button-to-stop-the-music-in-xcode/18509/3 "2016-09-23T05:04:01Z")

</div>

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()
    }
}

```
