title: Android: AudioRecord.read() returns garbage
body: <p>I've been working on an app that could playback audio from the microphone in real time.
  It sets up an AudioRecorder, that inits without any errors. However, it just returns a bunch of zeors, or a lot of number close to the max value of short, when performing read operations. I'm really stuck, it would be very kind if anyone could help me. This is my code:</p>
  
  <pre><code>public class AudioIn extends Thread {
  public static final int ERROR_RECORD_INIT = -1;
  public static final int ERROR_RECORD_NOTIFICATION = -2;
  public static final int ERROR_RECORD_READ = -3;
  public static final int SUCCESS = 0;
  
  public static final int audioFrequency = 44100;
  public static final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
  public static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
  final int ShortsReadPerCycle = 1024;
  
  
  private boolean capture = true;
  private AudioRecord recorder;
  private int effectiveCaptureBufferSize;
  private short[] buffer;
  
  private AudioInHandler handler;
  
  public AudioIn()
  {
      int minDeviceBuffer = AudioRecord.getMinBufferSize(audioFrequency, channelConfig, audioFormat);
      Log.d("AudioIn", "Minimum device capture buffer is: " + Integer.toString(minDeviceBuffer) + " bytes");
  
      effectiveCaptureBufferSize = minDeviceBuffer;
      Log.d("AudioIn", "Setting capture buffer size to " + effectiveCaptureBufferSize + " bytes");
  }
  
  public void close()
  {
      capture = false;
  }
  
  public int samplesPerBuffer()
  {
      return effectiveCaptureBufferSize / 2;
  }
  
  @Override
  public void run() {
      // TODO Auto-generated method stub
      android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
      try
      {
          recorder = new AudioRecord(AudioSource.MIC, audioFrequency, channelConfig, audioFormat, effectiveCaptureBufferSize);
      }
      catch (Throwable t)
      {
          handler.onError(ERROR_RECORD_INIT);
          return;         
      }
  
      recorder.startRecording();
  
      while(capture)
      {
          buffer = new short[ShortsReadPerCycle];
          int shortsRead = recorder.read(buffer, 0, buffer.length);
          if (shortsRead &lt; 0)
          {
              new Runnable() {
  
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      handler.onError(ERROR_RECORD_READ);
                  }
              }.run();
              this.close();
          }
          else
          {
              new Runnable() {
  
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      handler.onNewData(buffer);
                  }
              }.run();    
          }
      }
      Log.d("AudioIn.run()", "Stopping AudioRecord...");
      recorder.stop();
      Log.d("AudioIn.run()", "Stopped AudioRecord, now releasing...");
      recorder.release();
      Log.d("AudioIn.run()", "AudioRecord released");
      recorder = null;
  }
  
  public void setHandler(AudioInHandler handler) {
      this.handler = handler;
  }
  }
  </code></pre>