join Audio Files - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

join Audio Files

Demo Code


//package com.java2s;
import javax.sound.sampled.*;
import java.io.*;
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

public class Main {
    public static void joinAudioFiles(AudioFormat audioFormat,
            List<File> audioFiles, File output) throws IOException,
            UnsupportedAudioFileException {
        output.getParentFile().mkdirs();
        output.delete();/*from  w ww. j  a  va  2  s . c  o m*/
        output.createNewFile();

        List<AudioInputStream> audioInputStreams = new ArrayList<AudioInputStream>();
        long totalFrameLength = 0;
        for (File audioFile : audioFiles) {
            AudioInputStream fileAudioInputStream = AudioSystem
                    .getAudioInputStream(audioFile);
            audioInputStreams.add(fileAudioInputStream);
            totalFrameLength += fileAudioInputStream.getFrameLength();
        }

        AudioInputStream sequenceInputStream = new AudioInputStream(
                new SequenceInputStream(
                        Collections.enumeration(audioInputStreams)),
                audioFormat, totalFrameLength);
        AudioSystem.write(sequenceInputStream, AudioFileFormat.Type.WAVE,
                output);
    }
}

Related Tutorials