Java - 组合 2 个以上 .wav 文件时出现问题
对于我正在进行的项目,我希望能够连接多个 .wav 文件。
通过我的研究,我能够想出这段代码:
File sample1 = new File("F:\\Programming\\Resources\\Java_Sound\\trumpet1.wav");
File sample2 = new File("F:\\Programming\\Resources\\Java_Sound\\trumpet2.wav");
File fileOut = new File("F:\\Programming\\Resources\\Java_Sound\\Test.wav");
AudioInputStream audio1 = AudioSystem.getAudioInputStream(sample1);
AudioInputStream audio2 = AudioSystem.getAudioInputStream(sample2);
AudioInputStream audioBuild = new AudioInputStream(new SequenceInputStream(audio1, audio2), audio1.getFormat(), audio1.getFrameLength() + audio2.getFrameLength());
//for(int i = 0; i < 5; i++){
// audioBuild = new AudioInputStream(new SequenceInputStream(audioBuild, audio2), audioBuild.getFormat(), audioBuild.getFrameLength() + audio2.getFrameLength());
//}
AudioSystem.write(audioBuild, AudioFileFormat.Type.WAVE, fileOut);
它可以很好地组合两个 .wav 文件,但是当我取消注释 for 循环时,生成的 .wav 文件仅播放第一个串联的音频。音轨似乎提前结束,因为 wmp 的持续时间条仅在屏幕上移动了大约 1\5。
我假设问题出在创建的 .wav 文件中的标头。我研究了许多不同的网页,讨论如何构建标头,但它们的定义都略有不同,但都说标头应该是十六进制的。当转换流(不是音频流,标准 FileInputStream)时,我的标头是十进制的。另外,在 RIFF 部分之后、WAVE 部分之前,应该是整个文件的大小,不包括前 8 个字节。然而我的一些包含连字符。老实说我不知道这些是什么意思。然而,忽略它们,取消上面代码的注释后测试文件的大小仍然是一个更大的数字。
因此,在研究了如何连接多个音频文件以及如何创建\管理 .wav 标头之后,我仍然不知道为什么我的其余音频没有播放(如果它存在的话)。非常感谢任何帮助。提前致谢。
for a project I'm working on, I want to be able to concatenate multiple .wav files.
Through my research I was able to come up with this code:
File sample1 = new File("F:\\Programming\\Resources\\Java_Sound\\trumpet1.wav");
File sample2 = new File("F:\\Programming\\Resources\\Java_Sound\\trumpet2.wav");
File fileOut = new File("F:\\Programming\\Resources\\Java_Sound\\Test.wav");
AudioInputStream audio1 = AudioSystem.getAudioInputStream(sample1);
AudioInputStream audio2 = AudioSystem.getAudioInputStream(sample2);
AudioInputStream audioBuild = new AudioInputStream(new SequenceInputStream(audio1, audio2), audio1.getFormat(), audio1.getFrameLength() + audio2.getFrameLength());
//for(int i = 0; i < 5; i++){
// audioBuild = new AudioInputStream(new SequenceInputStream(audioBuild, audio2), audioBuild.getFormat(), audioBuild.getFrameLength() + audio2.getFrameLength());
//}
AudioSystem.write(audioBuild, AudioFileFormat.Type.WAVE, fileOut);
it works fine for combining two .wav files, however when I uncomment the for loop the produced .wav file only plays audio for the first concatenation. The audio track appears to end early, as wmp's duration bar only goes about 1\5 of the way across the screen.
I've assumed that the problem is with the header in the created .wav file. I've researched many different web pages discussing how a header in constructed, but all of them had slightly different definitions, but all said the header should be in hex. When converting the stream (not the audio stream, a standard FileInputStream) the headers I had were in decimal. Additionally, after the RIFF part, and before the WAVE part, is supposed to be the size of the whole file, not including the first 8 bytes. However some of mine included hyphens. To be honest I have no clue what those mean. Ignoring them however, the size of the test file after uncommenting the code above is still a larger number.
So after researching both how to concatenate multiple audio files, and how to create\manage .wav headers, I still have no clue why the rest of my audio isn't playing, if it even exists. Any help is greatly appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为输入流不能被多次读取。一旦您读取输入流,它将位于其末尾,并且尝试进一步读取将不会从该流中读取更多字节。
这应该只需稍加修改即可工作,继续在循环中创建新的音频输入流:
此外,请确保文件的音频格式完全相同。也就是说,相同的采样率、相同的通道数、每个样本的相同位数。否则,您将需要额外的代码来进行示例转换。
It might be because the input streams cannot be read more than once. Once you read an input stream, it will be at its end and attempt to read further will read no more bytes from that stream.
This should work with a slight modification, keep creating new audio input streams in your loop:
Also, ensure your audio formats for the files are exactly the same. That is, same sample rate, same channel count, same bits per sample. Otherwise you'll need additional code to do sample conversion.
这就是我用来加入任意数量的波形文件的方法。我循环遍历波形文件路径的字符串值列表,每次将上一个生成的 AudioInputStream 与下一个剪辑连接起来。
This is what I used to join any amount of wave files. I looped through a list of the string values for the wave file paths, and each time I join the previous resulting AudioInputStream with the next clip.