title: Android AudioTrack.write() returns after playing the whole buffer
body: <p>AudioTrack.write(). Android documentation says that </p>
  
  <blockquote>
    <p>In streaming mode, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count.</p>
  </blockquote>
  
  <p>However, in my code, write() method seems to wait until the whole buffer is played until the from the loudspeaker. Therefore, for example calling stop() method afterwards or filling more data is not possible.</p>
  
  <p>The audiotrack is initialized by:</p>
  
  <pre><code>int mBufferSize = AudioTrack.getMinBufferSize(44100,
          AudioFormat.CHANNEL_OUT_MONO,
          AudioFormat.ENCODING_PCM_8BIT);
  AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
          AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
          mBufferSize, AudioTrack.MODE_STREAM);
  int duration = 44100*5;
  short[] mBuffer = new short[duration];
  </code></pre>
  
  <p>In onCreate:</p>
  
  <pre><code>for (int i = 0; i &lt; wavelength; i++) {
          waveform[i] = Math.sin(((double) i)/wavelength * 2 * Math.PI - Math.PI);
  }
  mAudioTrack.play();
  </code></pre>
  
  <p>On a button click, this is called:</p>
  
  <pre><code>private void playSound(double frequency, int duration) {
      int idx = 0;
      for (int i = 0; i &lt; duration-1; i++) {
          idx = idx + (int) Math.ceil(frequency);
          if (idx &gt; wavelength-1)
              idx = idx % wavelength;
          mBuffer[i] = (short) (waveform[idx] * Short.MAX_VALUE);
      }
      long startTime = System.currentTimeMillis();
      ret = mAudioTrack.write(mBuffer, 0, mBuffer.length);
      long runtime = System.currentTimeMillis() - startTime;
      debugText.setText(Long.toString(runtime));
  }
  </code></pre>
  
  <p>The timestamp shows that the write() call takes exactly 5 seconds, which is the length of the audio clip, and I wouldn't believe the transfer time would be exactly the same. I want to generate more data to be played (perhaps with another frequency) while the previous data is still being played. I know some developers use multiple threads (and I have no experience in threading, so I don't know how to do it), but the documentation indicates it would be possible also this way...</p>