Dear All,
I’ve included a test program at the end, with a bug which seems
to have appeared in J2SE 1.5 (beta 2).
When I call the program with an ordinary WAV file, most of
the time nothing is played, although the program does wait
for the duration of the audio before exiting.
Compile the program:
javac PlayClip.java
Run:
java PlayClip dog.wav
Any WAV file will do.
- Andrew
` // PlayClip.java
import java.io.;
import javax.sound.sampled.;
public class PlayClip implements LineListener
{
private Clip clip = null;
public PlayClip(String fnm)
{
loadClip(fnm);
if (clip != null) {
System.out.println("Playing...");
clip.start(); // start playing
}
// wait for the sound to finish playing; guess at 10 mins!
System.out.println("Waiting");
try {
Thread.sleep(600000); // 10 mins in ms
}
catch(InterruptedException e)
{ System.out.println("Sleep Interrupted"); }
} // end of PlayClip()
private void loadClip(String fnm)
{
try {
// link an audio stream to the sound clip’s file
AudioInputStream stream = AudioSystem.getAudioInputStream(
getClass().getResource(fnm) );
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
// make sure sound system supports data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Unsupported Clip File: " + fnm);
System.exit(0);
}
// get clip line resource
clip = (Clip) AudioSystem.getLine(info);
// listen to clip for events
clip.addLineListener(this);
clip.open(stream); // open the sound file as a clip
stream.close(); // we're done with the input stream
} // end of try block
catch (Exception e) {
System.out.println(e);
System.exit(0);
}
} // end of loadClip()
public void update(LineEvent lineEvent)
// called when the clip’s line detects open, close, start, stop events
{
// has the clip has reached its end?
if (lineEvent.getType() == LineEvent.Type.STOP) {
System.out.println(“Exiting…”);
clip.stop();
System.exit(0);
}
} // end of update()
// --------------------------------------------------
public static void main(String[] args)
{
if (args.length != 1) {
System.out.println("Usage: java PlayClip ");
System.exit(0);
}
new PlayClip(args[0]);
} // end of main()
} // end of PlayClip.java
`