Concatenating WAV files in Java

后端 未结 3 811
一生所求
一生所求 2021-01-22 06:46

Here is my code that concatenates four wav files and produces wavAppended.wav. This concatenated file nicely plays in Windows Media Player. But through the PlaySound class, only

3条回答
  •  孤城傲影
    2021-01-22 07:12

    In above given example you need to use the SequenceInputStream then it will work fine. please find my code below to join two files.

    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 JoinWav{
    
            public static void main(String... args) throws Exception{
                    String wav_1 = "1497434542598100215.wav";
                    String wav_2 = "104860397153760.wav";
                    AudioInputStream stream_1 = AudioSystem.getAudioInputStream(new File(wav_1));
                    AudioInputStream stream_2 = AudioSystem.getAudioInputStream(new File(wav_2));
                    System.out.println("Info : Format ["+stream_1.getFormat()+"] Frame Length ["+stream_1.getFrameLength()+"]");
                    AudioInputStream stream_join = new AudioInputStream(new SequenceInputStream(stream_1,stream_2),stream_1.getFormat(),stream_1.getFrameLength()+stream_2.getFrameLength());
                    AudioSystem.write(stream_join,AudioFileFormat.Type.WAVE,new File("join.wav"));
            }
    
    }
    

提交回复
热议问题