62,623
社区成员
发帖
与我相关
我的任务
分享These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Loading and Playing Sampled Audio
Supported audio file formats: aif, au, and wav.
try {
// From file
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File("audiofile"));
// From URL
stream = AudioSystem.getAudioInputStream(
new URL("http://hostname/audiofile"));
// 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);
}
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(),
//The next two lines should be in one line.
((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();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
} catch (UnsupportedAudioFileException e) {
}