如何用Java播放声音?我希望能够在我的程序中播放声音文件。我该去哪看看?
4 回答

慕雪6442864
TA贡献1812条经验 获得超5个赞
.wav
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();}
添加回答
举报
0/150
提交
取消