title: Using AudioRecorder for streaming and MediaRecorder for recording does not allow Microphone Resources Sharing
body: <p>I'm trying to record Audio file in android with MediaRecorder at the same time to use AudioRecorder to Stream the recorded Audio to the Audio Track, But I think Android doesn't allow this two class to use the Microphone resources at the same time.</p>
  
  <p><strong>Here is a sample of my MediaRecorder Function</strong></p>
  
  <pre><code>private void recordAudio() {
      if (mMediaRecorder != null){
          mMediaRecorder.release();
          mMediaRecorder = null;
      }
      mMediaRecorder = new MediaRecorder();
      mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mMediaRecorder.setOutputFile(path);
      mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mMediaRecorder.setAudioEncodingBitRate(16);
      mMediaRecorder.setAudioSamplingRate(44100);
      mMediaRecorder.setAudioChannels(1);
  
      try {
          mMediaRecorder.prepare();
      } catch (IOException e) {
          e.printStackTrace();
      }
      mMediaRecorder.start();
  }
  </code></pre>
  
  <p><strong>Here is my Microphone Streaming Class</strong></p>
  
  <pre><code>public class MicrophoneStream {
  private static final String TAG = "MicrophoneStream";
  private AudioRecord mAudioRecord;
  private AudioTrack mAudioTrack;
  private Thread streamThread;
  private boolean isRecording;
  private int minBuffer;
  private int sampleRate = 16000;
  //Different file sample rate ---&gt; 8000, 11025, 16000, 22050, 44100
  
  public MicrophoneStream() {
  }
  
  private void stream(){
      if (mAudioTrack != null) {
          mAudioTrack.release();
          mAudioTrack = null;
      }
      if (mAudioRecord != null) {
          mAudioRecord.release();
          mAudioRecord = null;
      }
  
  
      minBuffer = AudioRecord.getMinBufferSize(sampleRate,
              AudioFormat.CHANNEL_IN_MONO,
              AudioFormat.ENCODING_PCM_16BIT);
  
      mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
              sampleRate,
              AudioFormat.CHANNEL_IN_MONO,
              AudioFormat.ENCODING_PCM_16BIT, minBuffer);
  
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
          mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                  sampleRate,
                  AudioFormat.CHANNEL_OUT_MONO,
                  AudioFormat.ENCODING_PCM_16BIT,
                  minBuffer,
                  AudioTrack.PERFORMANCE_MODE_LOW_LATENCY);
      } else {
          mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                  sampleRate,
                  AudioFormat.CHANNEL_OUT_MONO,
                  AudioFormat.ENCODING_PCM_16BIT,
                  minBuffer,
                  AudioTrack.MODE_STREAM);
      }
  
      mAudioTrack.setPlaybackRate(sampleRate);
  
      try {
          mAudioRecord.startRecording();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      }
  
      mAudioTrack.play();
  
      byte[] buffer = new byte[minBuffer];
  
      while (isRecording) {
          mAudioRecord.read(buffer, 0, buffer.length);
          //Log.d(TAG, "audioStreamer: " + buffer);
          mAudioTrack.write(buffer, 0, buffer.length);
      }
  }
  
  public void startStream() {
      //for mic streaming
      isRecording = true;
      streamThread = new Thread(new Runnable() {
          @Override
          public void run() {
              stream();
          }
      });
      streamThread.start();
  }
  
  public void stopStream() {
      isRecording = false;
      streamThread = null;
  }
  }
  </code></pre>
  
  <p>Both this of this function work fine but when I merge it, only one function work. </p>
  
  <p>When I execute the startStream function first the streaming start and then I got this Error:</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                              Process: com.tabz.swolo, PID: 13628
                                                              java.lang.IllegalStateException
                                                                  at android.media.MediaRecorder._start(Native Method)
                                                                  at android.media.MediaRecorder.start(MediaRecorder.java:873)
                                                                  at com.tabz.swolo.MergeRecordingActivity.recordAudio(MergeRecordingActivity.java:94)
                                                                  at com.tabz.swolo.MergeRecordingActivity.access$100(MergeRecordingActivity.java:25)
                                                                  at com.tabz.swolo.MergeRecordingActivity$1.onGrant(MergeRecordingActivity.java:59)
                                                                  at com.tabz.swolo.BASE_CLASS.isPermissionGranted(BASE_CLASS.java:30)
                                                                  at com.tabz.swolo.MergeRecordingActivity.onClick(MergeRecordingActivity.java:55)
                                                                  at android.view.View.performClick(View.java:5225)
                                                                  at android.view.View$PerformClick.run(View.java:21195)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>And when I execute the recording function first I got this error and no microphone streaming but the recording is working.</p>
  
  <pre><code> E/AudioRecord: start() status -38
  </code></pre>