I am currently developing an Android Application that has audio recording and playing. I am new to dealing with audio and I\'m having some trouble with encoding and formats.
I know it is late and you got your stuff working with MediaRecorder. But thought of sharing my answer as it took me some good time to find it. :)
When you record your audio, the data is read as short from your AudioRecord object and it is then converted to bytes before storing in the .pcm file.
Now, when you write the .wav file, you're again doing the short conversion. This is not required. So, in your code if you remove the following block and write the rawData directly to the end of .wav file. It will work just fine.
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);
}
Check the below piece of code you'll get after removing the duplicate block of code.
writeInt(output, rawData.length); // subchunk 2 size
// removed the duplicate short conversion
output.write(rawData);