I am working on a simple application which needs the facility to read in all the .wav files in a specified directory (/audiofiles), and then concatenate them. I have working code which gets the names of all the files in the directory and prints them to the console, and code which concatenates a list of specified files, but I cannot seem to combine the two functions. Any suggestions?
So far:-
import java.util.*;
import java.io.*;
import javax.sound.sampled.*;
public class getconc_1 {
public static void main(String[] args) {
// get list of file names from audio directory
File audDir = new File("/audiofiles");
//define a list to contain the audio files names and path
File[] filesList = audDir.listFiles();
// assign contents of each wav file from filesList to a string
// read the string from the audio file into an AudioInputStream, and concatenate
try {
long length = 0;
AudioInputStream clip = null;
List<AudioInputStream> list = new ArrayList<AudioInputStream>();
for (File file:filesList ) {
clip = AudioSystem.getAudioInputStream(new File(file.getPath()));
list.add(clip);
length += clip.getFrameLength();
}
if(length>0 && list.size()>0 && clip!=null) {
AudioInputStream appendedFiles =
new AudioInputStream(
new SequenceInputStream(Collections.enumeration(list)),
clip.getFormat(),
length);
AudioSystem.write(appendedFiles,
AudioFileFormat.Type.WAVE,
new File("wavAppended12.wav"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}