1
   class One {

    public static void main(String[] arg) {
        new One().play();
    }

    public void play() {
        try {
            AudioClip clip = Applet.newAudioClip(new URL(
                    "file:\\C:\\Documents and Settings\\admin\\Desktop\\javabottle-open.wav"));
            clip.play();
            URL ur = new URL("file:\\C:\\Documents and Settings\\admin\\Desktop\\flute+hrn+mrmba.aif");
            clip = Applet.newAudioClip(ur);
            clip.play();
            Thread.sleep(10000);
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

我没有收到任何异常消息,也没有播放音频。

4

1 回答 1

1

这对我有用

public class ClipTest {

    public void run() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        InputStream inRequest = this.getClass().getResourceAsStream("batR.wav");
        AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

        DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(sound);

        clip.addLineListener(new LineListener() {

            public void update(LineEvent event) {
                if(event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                    System.exit(0);
                }
            }
        });

        clip.start();

    }

    public static void main(String[] args) throws Exception {
        ClipTest clipTest = new ClipTest();
        clipTest.run();

    }
}
于 2013-02-19T09:21:55.370 回答