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

时间:2015-04-15 16:08:42

标签: java audio inputstream

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

1 个答案:

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