62,634
社区成员




public static void main(String[] args) {
Thread td = new Thread() {
public void run() {
try {
// From URL
AudioInputStream stream = AudioSystem
.getAudioInputStream(new File("C:\\TEMP\\a.wav"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, format
.getSampleRate(), format
.getSampleSizeInBits() * 2, format
.getChannels(),
format.getFrameSize() * 2, format
.getFrameRate(), true); // big
// endian
stream = AudioSystem
.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(Clip.class, stream
.getFormat(),
((int) stream.getFrameLength() * format
.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is
// completely
// loaded
clip.open(stream);
// Start playing
clip.start();
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
};
td.setDaemon(false);
td.start();
}