如何用Java播放声音?

如何用Java播放声音?

我希望能够在我的程序中播放声音文件。我该去哪看看?



MMMHUHU
浏览 2452回答 4
4回答

慕雪6442864

我编写了以下代码,运行良好。但我认为它只适用于.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();}

慕田峪9158850

这个声迹Java教程值得作为起点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java