title: Using same button to Play/Pause and loop audio in AVFoundation
body: <p>I have tried linking the action to the same button but this stops the button from working altogether. I can get it working using the same code if I create a separate button:</p>
  
  <pre><code>@IBAction func Play(sender: AnyObject) {
      SoundPlayer.play()
  }
  
  @IBAction func stop(sender: AnyObject) {        
      SoundPlayer.stop()
  }
  </code></pre>
  
  <p>I also need the audio to loop.</p>
  
  <p>Using Nirav code:</p>
  
  <pre><code>class ViewController: UIViewController {
  
     var SoundPlayer:AVAudioPlayer = AVAudioPlayer()
  
     @IBOutlet var btnPlay: UIButton!
     @IBOutlet var btnStop: UIButton!
  
     override func viewDidLoad() {
        super.viewDidLoad()
  
        let FileLocation = NSBundle.mainBundle().pathForResource("Aspirin", ofType: ".mp3")
  
  
        do {
            SoundPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: FileLocation!))
  
  
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
            try AVAudioSession.sharedInstance().setActive(true)
  
        }
  
        catch {
            print(error)
        }
  
     }
  
     override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
     }
  
     @IBAction func btnTap(sender: UIButton) {
        if (sender == self.btnPlay) {
        }
        else if (sender == self.btnStop) {
        }
     }
  }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/tdgan.png" rel="nofollow">1</a> - Screenshot of storyboard</p>