在java中如何实现声音,我如何在Java中播放声音?
I want to be able to play sound files in my program. Where should I look?
解決方案
I wrote the following code that works fine. But I think it only works with .wav format.
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
總結
以上是生活随笔為你收集整理的在java中如何实现声音,我如何在Java中播放声音?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java逻辑第九章_深入理解jvm-(第
- 下一篇: java中封装日期加时间_java日期处