java中播放声音的问题(不是applet)

abcdefg007 abcdefg007 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
java application中如何播放声音?

我找到一段代码
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);
}

// 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();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
} catch (UnsupportedAudioFileException e) {
}
参见:http://www.exampledepot.com/egs/javax.sound.sampled/Load.html
把其中audiofile改成sound.wav,
http://hostname/audiofile改成file:\\E:\\sound.wav,但运行并没有播放声音
是什么问题呢?

或者还有其他什么方法能实现?
谢谢!
...全文
评论 9
收藏 已收藏
分享
返回顶部
abcdefg007
Java SE 62,621 成员 307,251 内容