title: Recording .Wav with Android AudioRecorder
body: <p>I have read a lot of pages about Android's AudioRecorder. You can see a list of them below the question.</p>
  
  <p>I'm trying to record audio with AudioRecorder, but it's not working well.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  AudioRecord ar = null;
  int buffsize = 0;
  
  int blockSize = 256;
  boolean isRecording = false;
  private Thread recordingThread = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
  }
  
  public void baslat(View v)
  {
              // when click to START 
      buffsize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
      ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffsize);
  
      ar.startRecording();
  
      isRecording = true;
      recordingThread = new Thread(new Runnable() {
          public void run() {
              writeAudioDataToFile();
          }
      }, "AudioRecorder Thread");
      recordingThread.start();
  }
  public void durdur(View v)
  {
              // When click to STOP
      ar.stop();
      isRecording = false;
  }
  
  private void writeAudioDataToFile() {
      // Write the output audio in byte
  
      String filePath = "/sdcard/voice8K16bitmono.wav";
      short sData[] = new short[buffsize/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
  
          ar.read(sData, 0, buffsize/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, buffsize);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      try {
          os.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  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;
  
  }
  </code></pre>
  
  <p>It's creating a .wav file but, when I try to listen to it, it's not opening. I'm getting a "file not supported" error. I've tried to play the file with quite a few media player applications.</p>
  
  <p><strong>NOTE</strong> : I have to use AudioRecorder instead of MediaRecorder because my app will be doing another process while recording (displaying an equalizer) . </p>
  
  <p>Here is the list of pages that I've read about this subject:</p>
  
  <ol>
  <li><a href="http://developer.android.com/reference/android/media/AudioRecord.html#read(short[],%20int,%20int)" rel="noreferrer">http://developer.android.com/reference/android/media/AudioRecord.html#read(short[],%20int,%20int)</a></li>
  <li><a href="https://stackoverflow.com/questions/8499042/android-audiorecord-example">Android AudioRecord example</a></li>
  <li><a href="http://audiorecordandroid.blogspot.in" rel="noreferrer">http://audiorecordandroid.blogspot.in</a></li>
  <li><a href="https://stackoverflow.com/questions/4843739/audiorecord-object-not-initializing">AudioRecord object not initializing</a></li>
  <li><a href="https://stackoverflow.com/questions/4452815/recording-a-wav-file-from-the-mic-in-android-problems">Recording a wav file from the mic in Android - problems</a></li>
  <li><a href="http://i-liger.com/article/android-wav-audio-recording" rel="noreferrer">http://i-liger.com/article/android-wav-audio-recording</a></li>
  <li><a href="https://stackoverflow.com/questions/4777181/creating-a-wav-file-from-raw-pcm-data-using-the-android-sdk">Creating a WAV file from raw PCM data using the Android SDK</a></li>
  <li><a href="https://stackoverflow.com/questions/5511250/capturing-sound-for-analysis-and-visualizing-frequencies-in-android#5511250">Capturing Sound for Analysis and Visualizing Frequencies in Android</a></li>
  </ol>
  
  <p>There are a lot of different ways to go about this. I've tried lots of them but nothing works for me. I've been working on this problem for about 6 hours now so I would appreciate a definitive answer, ideally some sample code.</p>