javaaudiowavaudioinputstream

Why won't java play my .wav files?


Here is my very short test method:

public static void main(String[] args) {

try{
File sound = new File("./res/bird.wav");
AudioInputStream stream = AudioSystem.getAudioInputStream(sound);
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();

Thread.sleep(clip.getMicrosecondLength() / 1000);
}catch (Exception e){
    e.printStackTrace();
}}}

I get no errors, however the file doesn't play. I've tested with multiple different wav files, but none of them have been playing?

EDIT: Could it be something to do with me being on linux? Pulseaudio does not show any sound output. Also, I'm using Eclipse.


Solution

  • FIXED!

    Apparently PulseAudio and java don't get along by default.

    Here is how I fixed it:

    http://0110.be/posts/PulseAudio_Support_for_Sun_Java_6_on_Ubuntu

    Replace java6 with the current version, however. Started working as soon as I copied the jars.