title: Immediate Audio Input & Output Android
body: <p>In my Android App, I would like to take in some audio from the mic of the smartphone and play it immediately, live, like a microphone, with no lag. I am currently thinking of using <code>AudioRecord</code> and <code>AudioTrack</code> classes (from what I have read), but I'm not quite sure how to proceed.</p>
  
  <p>I checked out some other questions on Stack Overflow but they don't exactly answer what I would like to do. And most are from 2012.</p>
  
  <p>So how can I use these classes to input and output audio simultaneously?</p>
  
  <p><strong>ALSO:</strong> I had a look at the <code>MediaRecorder API</code>, but from what I read, that requires you to save the audio to a file, which I don't want to do. Can it be tweeked to meet my requirements? Or am I better off just using <code>AudioRecord</code>?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is my updated code below as @Pradip Pramanick suggested:</p>
  
  <pre><code>final Thread record = new Thread(new Runnable() {
          @Override
          public void run() {
              while (!Thread.interrupted()) {
                  MediaRecorder microphone = new MediaRecorder();
                  microphone.setAudioSource(MediaRecorder.AudioSource.MIC);
                      microphone.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                  microphone.setOutputFile(filename);
                  microphone.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                  try {
                      microphone.prepare();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  microphone.start();
              }
          }
      });
  
      final Thread play = new Thread(new Runnable() {
          @Override
          public void run() {
              while (!Thread.interrupted()) {
                  player = new MediaPlayer();
                  try {
                      player.setDataSource(filename);
                      player.prepare();
                      player.start();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      });
  </code></pre>
  
  <p>I am getting an <code>Illegal State Exception | Start failed: -38</code>. But I am calling <code>microphone.start</code> after <code>microphone.prepare</code>... What seems to be the problem? I searched other threads which said there might be other background apps using the microphone. I searched my device: Moto X Play (3rd Gen) and found none. (I even turned off "Ok Google" voice recognition, but the error kept coming).</p>
  
  <p><strong>ERROR LOG:</strong></p>
  
  <p>Here is the log-cat showing the most recent errors:</p>
  
  <pre><code>01-31 09:37:21.064 344-3339/? E/MediaPlayerService: offset error
  01-31 09:37:21.065 1835-1922/com.synerflow.testapp E/MediaPlayer: Unable to create media player
  
  01-31 09:37:21.065 1835-1922/com.synerflow.testapp I/Player: player.prepare() has failed
  01-31 09:37:21.065 1835-1922/com.synerflow.testapp W/System.err: java.io.IOException: setDataSourceFD failed.: status=0x80000000
  </code></pre>
  
  <p>The IO Exception seems to be at <code>player.setDataSource(filename)</code>, the filename variable is a string: <code>Environment.getExternalStorageDirectory().getAbsolutePath() + "\voice.3gp"</code></p>