I'm trying to write a .wav file from bytearray[] by adding the header of wave file and playing the written .wav file later. Null value is returned while saving it to .wav file? Can some one please point out the mistake i am committing? The below mentioned function generates a wave file but writes a null value in the file?
Can arraylist` be used for writing .wav file instead of bytearray[]?
// write out the wav file
public boolean save() {
try {
DataOutputStream outFile = new DataOutputStream(new
FileOutputStream("cartoon-birds_Result.wav"));
// write the wav file per the wav file format
outFile.writeBytes("RIFF"); // 00 - RIFF
outFile.write(shortToByteArray((short) myChunkSize), 0, 4);
// 04 - how big is the rest of this file?
outFile.writeBytes("WAVE"); // 08 - WAVE
outFile.writeBytes("fmt "); // 12 - fmt
outFile.write(shortToByteArray((short) mySubChunk1Size), 0, 4);
// 16 - size of this chunk
outFile.write(shortToByteArray((short) myFormat), 0, 2);
// 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
outFile.write(shortToByteArray((short) myChannels), 0, 2); // 22
- mono or stereo? 1 or 2? (or 5 or ???)
outFile.write(shortToByteArray((short) mySampleRate), 0, 4);
// 24 - samples per second (numbers per second)
outFile.write(shortToByteArray((short) myByteRate), 0, 4);
// 28 - bytes per second
outFile.write(shortToByteArray((short) myBlockAlign), 0, 2);
// 32 - # of bytes in one sample, for all channels
outFile.write(shortToByteArray((short) myBitsPerSample), 0, 2);
// 34 - how many bits in a sample(number)? usually 16 or 24
outFile.writeBytes("data"); // 36 - data
outFile.write(shortToByteArray((short) myDataSize), 0, 4);
// 40 - how big is this data chunk
outFile.write(myData); // 44 - the actual
data itself - just a long string of numbers
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
I am basically looking how to write bytearray back to .wav file which can be played