The following code is configured to bring up a blank JFrame, which the user can close after the sound clip has played. You may choose to put in a waiting time instead, by changing the main program to
AudioTest inst = new AudioTest(false); inst.test(filename); inst.sleep(3500);If the wait time is too short the program will exit before the end of the sound clip.
There also seems to be a problem with .au files. Java throws
a javax.sound.sampled.LineUnavailableException for format
ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame.
You can use a code converter such as AUD-to-WAV to convert
.au files to .wav format.
AudioTest.javaimport javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
import java.net.URL;
public class AudioTest extends JFrame
{
AudioTest(boolean app)
{
if (app) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Test Sound Clip");
setSize(300, 200);
setVisible(true);
}
}
public void test(String filename)
{
try{
URL url = getClass().getClassLoader().getResource(filename);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
AudioFormat audioFormat = audioIn.getFormat();
System.out.println(audioFormat);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
}catch(Exception e)
{
System.err.println(e);
}
}
public void sleep(int millisec) {
try {
Thread.sleep(millisec);
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String[] args)
{
String filename = "welcome.wav";
if (args.length>0) filename = args[0];
AudioTest inst = new AudioTest(true);
inst.test(filename);
//inst.sleep(3500);
}
}
Maintained by John Loomis, updated Thu Apr 06 15:48:14 2017