Ok, here is a pared down version of Game and SoundFactory. The code hangs in the Game constructor on creating a JFrame. The same hang happened with all sorts of other classes -- it doesn't have anything to do with JFrame. If I do not call the SoundFactory loadClip method, the JFrame is created. Most puzzling is this: when I leave in the call to loadClip, but also create a reference to a static constant, everything works fine!??!? This only works if a actually assign the field to a static constant: just declaring it doesn't work.
Any insights would be appreciated.
public class Game {
//Color x = Color.BLACK;
public Game() {
File b = new File("resources/Boom.wav");
SoundFactory.loadClip(b, 3);
System.out.println("About to create JFrame");
JFrame jf = new JFrame();
//following line doesn't happen unless I uncomment Color, above
System.out.println("Created JFrame");
}
public static void main (String [] args) {
new Game ();
}
}
public class SoundFactory {
private static HashMap availableClips;
static {
availableClips = new HashMap();
}
public static void loadClip (File soundFile, int howManyCopies) {
try {
for (int x = 0; x < howManyCopies; x++) {
InputStream is = new FileInputStream(soundFile);
loadClip (is, soundFile.getName());
is.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void loadClip (InputStream is, String name) {
if (!availableClips.containsKey(name)) {
availableClips.put(name, new LinkedList());
}
List list = (List) availableClips.get(name);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(is);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = null;
try {
clip = (Clip) AudioSystem.getLine(info);
clip.open(audioInputStream);
list.add(clip);
audioInputStream.close();
}
catch (LineUnavailableException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}