Java Sound; multipul clips
Hi, I'm trying to have a thread that will be able to load multiple clips and add them to a list of clips.
When I run my program my first clip loads with no problem, however when I invoke clip.open(AudioInputStream) for the second clip i get the following error: java.lang.OutOfMemoryError: Java heap space
My code is below:
/**
* This class is a Thread and is responsible for loading each clip, one at a time
* so the threading is so that it does not interfer with playback.
**/
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
publicclass SongLoaderextends LinkedList<File>implements Runnable{
List<Clip> clipList;
boolean running =false;
public SongLoader(List<Clip> cList){
super();
clipList = cList;
}
publicboolean offer(File f){
boolean result = super.offer(f);
if(!running){
new Thread(this).start();
running =true;
}
return result;
}
publicvoid run(){
while(size() != 0){
System.out.println("Loading: " + peek().toString());
System.out.println("\tFree Memory: " + Runtime.getRuntime().freeMemory());
AudioInputStream aInput = getAudioInputStream(remove());
System.out.println("\t\tGot AudioInputStream");
try{
Clip loading = AudioSystem.getClip();
System.out.println("\t\tGot Clip from AudioSystem");
loading.open(aInput);
System.out.println("\t\tLoading Started");
clipList.add(loading);
System.out.println("\tLoading Complete; Free Memory: " + Runtime.getRuntime().freeMemory());
aInput.close();
System.out.println("\t\tClosed AudioInputStream");
}catch(Exception e){
System.out.println("Error in SongLoader: " + e);
}
}
running =false;
}
private AudioInputStream getAudioInputStream(File f){
AudioInputStream aIn =null;
try{
BufferedInputStream dataIn =new BufferedInputStream(new
FileInputStream(f));
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(dataIn);
AudioFormat format = fileFormat.getFormat();
aIn =new AudioInputStream(dataIn, format, fileFormat.getByteLength());
}catch(Exception e){
System.out.println("Error in Songloader: " + e);
System.exit(0);
}
return aIn;
}
}
Thank you for your help.

