尝试更改将多个音频文件合并为一个文件以处理两个以上文件的代码

Trying to change code that combines multiple audio files into one to work for more than two files

使用 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));

您可以使用接受 EnumerationSequenceInputStream constructor overload 来做到这一点。

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);