Reliably playing a short sound in Java

后端 未结 6 1429
时光说笑
时光说笑 2021-01-06 04:46

I am tyring to write some Java code that basically just plays a short .wav file - with \'short\' I mean a fraction of a second. (The file I use is at /usr/share/sounds/gener

6条回答
  •  佛祖请我去吃肉
    2021-01-06 05:02

    I have had good luck using the following code in an application (even though it uses the Applet's newAudioClip() method):

    AudioClip clip;
    
    File fileClip = new File("/usr/share/sounds/generic.wav");
    
    URL url = null;
    
    try
    {
        URI uri = fileClip.toURI();
        url = uri.toURL();
        clip = Applet.newAudioClip(url);
    }
    catch (MalformedURLException e){}
    
    clip.play();
    

    I got this method from: Starting Out with Java: From Control Structures Through Objects, 4th Edition by Tony Gaddis, Addison Wesley, ISBN-13 978-0-13-608020-6

提交回复
热议问题