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
String wavFile1 = filesList.get(0);
String wavFile2 = filesList.get(1);
// read the string from the audio file into an AudioInputStream, and concatenate
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("wavAppended.wav"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
First of all you have error in your codes
String wavFile1 = filesList.get(0);
String wavFile2 = filesList.get(1);
Should be replaced by
String wavFile1 = filesList[0].getPath();
String wavFile2 = filesList[1].getPath();
Secondly use
new SequenceInputStream(Collections.enumeration(list)) to join multiple streams into one. I have not added few checks in the program like checking the size and null check for clip object.
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.