从 Java 中加入两个 WAV 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/653861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Join two WAV files from Java?
提问by krosenvold
采纳答案by James Van Huis
Here is the barebones code:
这是准系统代码:
import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class WavAppender {
public static void main(String[] args) {
String wavFile1 = "D:\wav1.wav";
String wavFile2 = "D:\wav2.wav";
try {
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));
AudioInputStream appendedFiles =
new AudioInputStream(
new SequenceInputStream(clip1, clip2),
clip1.getFormat(),
clip1.getFrameLength() + clip2.getFrameLength());
AudioSystem.write(appendedFiles,
AudioFileFormat.Type.WAVE,
new File("D:\wavAppended.wav"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
回答by schnaader
The WAV header should be not be too hard to parse, and if I read this header descriptioncorrectly, you can just strip the first 44 bytes from the second WAV and simply append the bytes to the first one. After that, you should of course change some of the header fields of the first WAV so that they contain the correct new length.
WAV 标头应该不会太难解析,如果我正确阅读了此标头描述,您只需从第二个 WAV 中去除前 44 个字节,然后简单地将字节附加到第一个。之后,您当然应该更改第一个 WAV 的一些头字段,以便它们包含正确的新长度。
回答by McDowell
回答by ReinstateMonica Larry Osterman
Your challenge though occurs if the two WAV files don't have the exact same format in the wave header.
如果两个 WAV 文件在波形标头中没有完全相同的格式,则会出现您的挑战。
If the wave formats on the two files aren't the same, you're going to have to find a way to transmogrify them so they match.
如果两个文件上的波形格式不同,您将不得不找到一种方法来变形它们以使它们匹配。
That may involve an MP3 transcode or other kinds of transcoding (if one of them is encoded with an MP3 codec).
这可能涉及 MP3 转码或其他类型的转码(如果其中之一使用 MP3 编解码器进行编码)。