title: Making precise android metronome
body: <p>I'm making an app for Android and in it's core it has a metronome.
  I need to produce sounds precisly in beat. And every 2nd sound is delayed for "swing" percentage of interval.
  My current code looks like this:</p>
  
  <pre><code>public void play() {
      mainCountHandler.postDelayed(mainClick, 0);
      subCountHandler.postDelayed(subClick, (MILLIS_IN_MINUTE/ (2 * bpm)) * (100 + swing));
  }
  
  private Runnable mainClick = new Runnable()
  {
      public void run()
      {
          playSound();
          mainCountHandler.postDelayed(this, MILLIS_IN_MINUTE / bpm);
      }
  };
  
  private Runnable subClick = new Runnable()
  {
      public void run()
      {
          playSound();
          subCountHandler.postDelayed(this, MILLIS_IN_MINUTE / bpm);
      }
  };
  
  private void  playSound() {
      final MediaPlayer mp = MediaPlayer.create(context, SOUND);
      mp.start();
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
          public void onCompletion(MediaPlayer mp) {
              mp.release();
          };
      });
  
      ++currentBeat;
      if (currentBeat &gt;= beatsCount) {
          if (loop) {
              currentBeat = 0;
          } else {
              stop();
          }
      }
  }
  </code></pre>
  
  <p>And this runs terribly inaccuate. I tried <b>SoundPool</b> and also creating <b>MediaPlayer</b> outside of <b>playSound</b> and there's no singnificant changes. 
  So is there any modifications or maybe another Android classes that can help me?</p>