Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Concatenation of Audio files

Diddel ParaFeb 27 2018 — edited Feb 27 2018

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();
  
}
}

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2018
Added on Feb 27 2018
0 comments
922 views