title: Android write PCM byte array to wav file
body: <p>I am trying to convert PCM 16 bit with Sampling rate 8000, single channel to wav file.</p>
  
  <p>below is code :</p>
  
  <pre><code>private void rawToWaveafterUpload(final File rawFile, final File waveFile) throws IOException {
  
          byte[] rawData = new byte[(int) rawFile.length()];
          DataInputStream input = null;
          if(rawFile.exists()) {
  
              try {
                  FileInputStream rawFileInputStream = new FileInputStream(rawFile);
                  input = new DataInputStream(rawFileInputStream);
                  input.read(rawData);
  
              } catch (Exception e) {
  
              } finally {
                  if (input != null) {
                      input.close();
                  }
              }
  
              DataOutputStream output = null;
              try {
                  output = new DataOutputStream(new FileOutputStream(waveFile));
                  // WAVE header
                  writeString(output, "RIFF"); // chunk id
                  writeInt(output, 36 + rawData.length); // chunk size
                  writeString(output, "WAVE"); // format
                  writeString(output, "fmt "); // subchunk 1 id
                  writeInt(output, 16); // subchunk 1 size
                  writeShort(output, (short) 1); // audio format (1 = PCM)
                  writeShort(output, (short) 1); // number of channels
                  writeInt(output, 8000); // sample rate
                  writeInt(output, 8000 * 2); // byte rate
                  writeShort(output, (short) 2); // block align
                  writeShort(output, (short) 16); // bits per sample
                  writeString(output, "data"); // subchunk 2 id
                  writeInt(output, rawData.length); // subchunk 2 size
                  // Audio data (conversion big endian -&gt; little endian)
                  short[] shorts = new short[rawData.length / 2];
                  ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                  ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
                  for (short s : shorts) {
                      bytes.putShort(s);
                  }
                  output.write(bytes.array());
              } finally {
                  if (output != null) {
                      output.close();
                      //rawFile.delete();
                  }
              }
  
          }}
  
  
      private void writeInt(final DataOutputStream output, final int value) throws IOException {
          output.write(value &gt;&gt; 0);
          output.write(value &gt;&gt; 8);
          output.write(value &gt;&gt; 16);
          output.write(value &gt;&gt; 24);
      }
  
      private void writeShort(final DataOutputStream output, final short value) throws IOException {
          output.write(value &gt;&gt; 0);
          output.write(value &gt;&gt; 8);
      }
  
      private void writeString(final DataOutputStream output, final String value) throws IOException {
          for (int i = 0; i &lt; value.length(); i++) {
              output.write(value.charAt(i));
          }
      }
  </code></pre>
  
  <p>When i play my pcm from <a href="https://play.google.com/store/apps/details?id=com.dn.pcmplayer&amp;hl=en" rel="nofollow noreferrer">PCM player App</a> the audio is playing fine. but i am not able to convert this audio to wav file. Audio is not getting played. below is <a href="https://quickfileshare.org/2FNz/left.pcm" rel="nofollow noreferrer">PCM file which i need to convert to wave</a></p>