Playing .wav file in java from file from computer

时光怂恿深爱的人放手 提交于 2019-11-29 18:06:45

First Attempt

AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath())); 

This is not how you construct a URL to a File. Instead, you should use File#getURI#getURL

AudioClip clip1 = Applet.newAudioClip(new File("/full/path/to/audio.wav").toURI().toURL());

Second Attempt

mediafire is returning a html response, not the audio file...You can test it with...

URL url = new URL("http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");    
try (InputStream is = url.openStream()) {
    int in = -1;
    while ((in = is.read()) != -1) {
        System.out.print((char)in);
    }
} catch (IOException exp) {
    exp.printStackTrace();
}

Third Attempt

You open the clip, but never start it...

URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
clip2.open(ais2);
clip2.start();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!