title: Stream microphone to speakers android
body: <p>I have been having troubles trying to figure out how to stream the microphone to the speakers with android.</p>
  
  <p>I would like to have a button that turns it on and off.</p>
  
  <p>I found some code, but getting it to work is the issue. If I run this code from a button, it crashes the app. It seems hard to just play the sound from the microphone to the speakers. </p>
  
  <p>Thank you all for your help in advance!</p>
  
  <pre><code>import android.media.AudioFormat;
  import android.media.AudioManager;
  import android.media.AudioRecord;
  import android.media.AudioTrack;
  import android.media.MediaRecorder;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
      // Variables
      private static final String TAG = "Aufnahme";
      private AudioRecord recorder = null;
      private boolean isRecording = false;
      private int SAMPLERATE = 8000;
      private int CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
      private int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
      private int bufferSize = AudioRecord.getMinBufferSize(SAMPLERATE,           CHANNELS, AUDIO_FORMAT);
  private Thread recordingThread = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
  
              SetupRecordButton();
          }
      });
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  public void SetupRecordButton(){
  
      Button recordButton = (Button) findViewById(R.id.button);
  
      recordButton.setOnClickListener(new View.OnClickListener(){
  
          @Override
          public void onClick(View v) {
              startRecording();
          }
      });
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  public void startRecording() {
      recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE,
              CHANNELS, AUDIO_FORMAT, bufferSize);
  
      recorder.startRecording();
      isRecording = true;
  
      recordingThread = new Thread(new Runnable()
  
      {
          public void run() {
              writeAudioData();
          }
  
      });
  
  
      //Start recording thread
      recordingThread.start();
  
  }
  
  public void stopRecording() {
      isRecording = false;
      recorder.stop();
      recorder.release();
      recorder = null;
      recordingThread = null;
  }
  
  private void writeAudioData() {
  
      byte data[] = new byte[bufferSize];
  
      while (isRecording) {
  
          recorder.read(data, 0, bufferSize);
          send(data);
  
      }
  }
  
  private void send(byte[] data) {
  
      int minBufferSize = AudioTrack.getMinBufferSize(8000,
              AudioFormat.CHANNEL_CONFIGURATION_MONO,
              AudioFormat.ENCODING_PCM_16BIT);
  
      AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
              AudioFormat.CHANNEL_CONFIGURATION_MONO,
              AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
              AudioTrack.MODE_STREAM);
  
      at.play();
      at.write(data, 0, bufferSize);
      at.stop();
      at.release();
  
      }
  }
  </code></pre>