title: How to record audio using AudioRecorder in Android
body: <p>I want to capture audio from an Android device. My code below seems to successfully make a <em>wav</em> file on the SD card but it cannot be played. I tried to play it using different media players but none work. There is an issue in  my code that is causing this problem.</p>
  
  <p>code </p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      private static final String LOG_TAG = "AudioRecordTest";
  
      static final int AUDIO_PORT = 2048;
      static final int SAMPLE_RATE = 8000;
      static final int SAMPLE_INTERVAL = 20; // milliseconds
      static final int SAMPLE_SIZE = 2; // bytes per sample
      static final int BUF_SIZE = SAMPLE_INTERVAL * SAMPLE_INTERVAL * SAMPLE_SIZE * 2;
  
      private static int[] mSampleRates = new int[]{44100, 44056, 47250, 48000, 22050, 16000, 11025, 8000};
      private Thread recordingThread = null;
      private boolean isRecording = false;
  
      int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we
      // use only 1024
      int BytesPerElement = 2; // 2 bytes in 16bit format
  
      private int bufferSize;
  
      private AudioRecord recorder;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          startRecording();
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  
      private void startRecording() {
  
          recorder = findAudioRecord();
  
          recorder.startRecording();
          isRecording = true;
          recordingThread = new Thread(new Runnable() {
              public void run() {
                  writeAudioDataToFile();
              }
          }, "AudioRecorder Thread");
          recordingThread.start();
      }
  
      // convert short to byte
      private byte[] short2byte(short[] sData) {
          int shortArrsize = sData.length;
          byte[] bytes = new byte[shortArrsize * 2];
          for (int i = 0; i &lt; shortArrsize; i++) {
              bytes[i * 2] = (byte) (sData[i] &amp; 0x00FF);
              bytes[(i * 2) + 1] = (byte) (sData[i] &gt;&gt; 8);
              sData[i] = 0;
          }
          return bytes;
  
      }
  
      public AudioRecord findAudioRecord() {
          for (int rate : mSampleRates) {
              for (short audioFormat : new short[]{AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT}) {
                  for (short channelConfig : new short[]{AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO}) {
                      try {
                          Log.d(LOG_TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                                  + channelConfig);
                          bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
  
                          if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                              // check if we can instantiate and have a success
                              recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
                              if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                                  return recorder;
                          }
                      } catch (Exception e) {
                          Log.e(LOG_TAG, rate + "Exception, keep trying.", e);
                      }
                  }
              }
          }
          return null;
      }
  
      private void writeAudioDataToFile() {
          /*// Write the output audio in byte
          short sData[] = new short[BufferElements2Rec];
  
          while (isRecording) {
              // gets the voice output from microphone to byte format
              recorder.read(sData, 0, BufferElements2Rec);
              System.out.println("Short wirting to file" + sData.toString());
              // // stores the voice buffer
              byte bData[] = short2byte(sData);
              sendLiveAudio(bData);
          }*/
  
          String filePath = "/sdcard/test.wav";
          short sData[] = new short[bufferSize / 2];
  
          FileOutputStream os = null;
          try {
              os = new FileOutputStream(filePath);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
  
          while (isRecording) {
              // gets the voice output from microphone to byte format
  
              recorder.read(sData, 0, bufferSize / 2);
              Log.d("eray", "Short wirting to file" + sData.toString());
              try {
                  // // writes the data to file from buffer
                  // // stores the voice buffer
                  byte bData[] = short2byte(sData);
                  os.write(bData, 0, bufferSize);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          try {
              os.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private void stopRecording() {
          // stops the recording activity
          if (null != recorder) {
              isRecording = false;
              recorder.stop();
              recorder.release();
              recorder = null;
              recordingThread = null;
          }
      }
  
      @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();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  
      @Override
      public void onBackPressed() {
          super.onBackPressed();
          stopRecording();
      }
  
  
  }
  </code></pre>