2

I used the code from Join two WAV files from Java. I tested it, however it only worked without a loop. I now did a loop to append multiple audio files, like this:

        AudioInputStream clip1 = null, clip2 = null;
        for (int i = 1; i < fileAudio.size(); i++){
            try {
                if (i == 1) {
                    clip1 = AudioSystem.getAudioInputStream(new File("audio/" + fileAudio.get(0))); 
                }else{
                    clip1 = AudioSystem.getAudioInputStream(new File("merged.wav"));
                }
                clip2 = AudioSystem.getAudioInputStream(new File("audio/" + fileAudio.get(i)));   

                AudioInputStream appendedFiles = 
                                new AudioInputStream(
                                    new SequenceInputStream(clip1, clip2),     
                                    clip1.getFormat(), 
                                    clip1.getFrameLength() + clip2.getFrameLength());
                AudioSystem.write(appendedFiles,
                                AudioFileFormat.Type.WAVE,
                                new File("merged.wav"));
                System.out.println("Appending audio:" + clip1.getFrameLength());
                clip1.close();
                clip2.close();
                appendedFiles.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

The result given is the correct length of audio files combined, but the sound is only from the last audio file. Meaning it didn't truly append the audio of the file(s).

Community
  • 1
  • 1
Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39
  • "The result given is correct only the lenght of audio" - to clarify, the length of the merged audio file is equal to the combined length of the other files? – Emz Nov 14 '15 at 03:54
  • @Emz Yes, lenght of audio is a total lenght of all audio files. But When I play the sound, it plays only the sound of the last audio in the first 15second. – Lay Leangsros Nov 14 '15 at 03:58
  • @LayLeangsros Have a look at this http://www.jsresources.org/examples/AudioConcat.html – Michael Queue Nov 17 '15 at 23:14
  • @MichaelQuatrani No, I haven't. I will check it. Thanks. – Lay Leangsros Nov 18 '15 at 01:50

0 Answers0