我试图通过添加wave文件的标题并稍后播放写入的.wav文件来编写来自bytearray []的.wav文件。保存为.wav文件时返回空值?有人可以指出我犯的错误吗?下面提到的函数生成一个wave文件,但在文件中写入空值?
可以使用arraylist`来编写.wav文件而不是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;
}
我基本上在寻找如何将bytearray写回可以播放的.wav文件
答案 0 :(得分:-1)
您不必构建自己的标头。 AudioSystem.write
方法可以为您处理此问题。
您是否了解Oracle" Sound Trail"教程。页面Using Files and Format Converters包含有关保存到文件的部分。请参阅中途点上方标题写入声音文件一节。
当我这样做时,我采取了以下步骤:
InputStream
的类,其中read
方法获取PCM数据并将其转换为与音频格式所需的字节值对应的整数值; InputStream
类作为AudioInputStream
的参数(以及音频格式和帧长度); AudioInputStream
作为AudioSystem.write
方法的参数,以及目标文件类型和文件。虽然你的起点是一个字节数组,而不是PCM,你仍然可以制定类似的计划。