/**
* Plays the file for duration milliseconds or loops.
*/
private void play(Project project, File file, int loops, Long duration) {
Clip audioClip = null;
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(file);
} catch (UnsupportedAudioFileException uafe) {
project.log("Audio format is not yet supported: "
+ uafe.getMessage());
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (audioInputStream != null) {
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format,
AudioSystem.NOT_SPECIFIED);
try {
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioInputStream);
} catch (LineUnavailableException e) {
project.log("The sound device is currently unavailable");
return;
} catch (IOException e) {
e.printStackTrace();
}
if (duration != null) {
playClip(audioClip, duration.longValue());
} else {
playClip(audioClip, loops);
}
audioClip.drain();
audioClip.close();
} else {
project.log("Can't get data from file " + file.getName());
}
}