title: Simple low-latency audio playback in iOS Swift
body: <p>I'm a beginner in iOS and I'm trying to design a drum set app with Swift. I designed a view with a single button and wrote the code below, but it has some problems:</p>
  
  <ol>
  <li>Some sounds are lost when I touch the button quickly like a drum roll.</li>
  <li>Still in a 'drum roll', the sound is interrupted every time the button is touched, instead of letting the sample play till its ending. It's awful in cymbal roll, for instance. I'd like to hear every sample sounding completely, even if I touch the button again.</li>
  <li>There is a latency between the touch and the sound. I know that <code>AVAudioPlayer</code> is not the best choice for low-latency audio, but as a beginner, it's hard to learn <code>OpenAL</code>, <code>AudioUnit</code> without code samples or tutorials in <code>Swift</code>. The problem is similar to this: <a href="https://stackoverflow.com/questions/14513095/which-framework-should-i-use-to-play-an-audio-file-wav-mp3-aiff-in-ios-with">Which framework should I use to play an audio file (WAV, MP3, AIFF) in iOS with low latency?</a>.</li>
  </ol>
  
  <p>The code:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      // Enable multiple touch for the button
      for v in view.subviews {
          if v.isKindOfClass(UIButton) {
              v.multipleTouchEnabled = true
          }
      }
  
      // Init audio
      audioURL = NSBundle.mainBundle().URLForResource("snareDrum", withExtension: "wav")!
      do {
          player = try AVAudioPlayer(contentsOfURL: audioURL)
          player?.prepareToPlay()
      } catch {
          print("AVAudioPlayer Error")
      }
  }
  
  override func viewDidDisappear(animated: Bool) {
      super.viewDidDisappear(animated)
  
      player?.stop()
      player = nil
  }
  
  @IBAction func playSound(sender: UIButton) {
      player?.currentTime = 0
      player?.play()
  }
  </code></pre>