使用jjmpeg,我有这个代码,它将两个wav文件合并为一个:
String wavFile1 = "C:/Users/Adela/Desktop/AerLingusPrototype2/src/examples/test.wav";
String wavFile2 = "C:/Users/Adela/Desktop/AerLingusPrototype2/src/examples/test2.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("C:/Users/Adela/Desktop/AerLingusPrototype2/src/examples/ring3.wav"));
} catch (Exception e) {
e.printStackTrace();
}
所以现在我有这样的多个wav文件,我该如何更改代码?在这种情况下,我将文件的路径存储为字符串
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(startAudio));
AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(flightNumberDigit1Audio));
AudioInputStream clip3 = AudioSystem.getAudioInputStream(new File(flightNumberDigit2Audio));
AudioInputStream clip4 = AudioSystem.getAudioInputStream(new File(flightNumberDigit3Audio));
AudioInputStream clip5 = AudioSystem.getAudioInputStream(new File(departingAtAudio));
AudioInputStream clip6 = AudioSystem.getAudioInputStream(new File(hoursAudio));
AudioInputStream clip7 = AudioSystem.getAudioInputStream(new File(minutesAudio));
AudioInputStream clip8 = AudioSystem.getAudioInputStream(new File(nowClosingAudio));
答案 0 :(得分:1)
您可以使用接受Enumeration
的{{3}}来执行此操作。
Vector<InputStream> inputStreams = new Vector<InputStream>();
inputStreams.add(clip1);
inputStreams.add(clip2);
inputStreams.add(clip3);
// etc...
// add up the lengths
int length = 0;
for (int i = 0 ; i < inputStreams.size(); ++i)
length += inputStreams.get(i).getFrameLength();
AudioInputStream appendedFiles =
new AudioInputStream(
new SequenceInputStream(inputStreams.elements()),
clip1.getFormat(), length);