tags:

views:

150

answers:

2

I have a Java application whose UI relies heavily on audio. On Windows and OS X, everything works fine; on Linux, however, the application requires exclusive access to the sound device, a LineUnavailableException is thrown and no sound is heard. I'm using Kubuntu 9.10.

This means that no other application can play audio while the program is running, and can't even be holding an audio device when the program starts. This is naturally unacceptable.

Here is the code I'm using to play audio:

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);

Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);

clip.start();

this.wait((clip.getMicrosecondLength() / 1000) + 100);

clip.stop();

Am I doing something wrong? Is using Java to play audio in Linux a lost cause?

+3  A: 

I fear that audio in Linux is a lost cause itself. But in this case, it really is a known Java Bug. You should try to figure out what sound architecture you are using. I think the default for Ubuntu is PulseAudio/ALSA. I'm not not sure about Kubuntu though.

There is a known workaround (I never tried it myself though).

It's also possible that some other applications you're running is exclusively using the soundcard, so make sure to test with different applications (i.e. applications that play nicely with others).

sfussenegger
+1  A: 

Java Sound is terrible for high-precision or low-latency tasks, and almost totally dysfunctional on Linux. Abandon ship now before you sink more time into it. After Java Sound I tried OpenAL which wasn't great on Linux either. Currently I'm using FMOD which is unfortunately closed-source.

The open source way to go would probably be PortAudio. Try talking to the SIP Communicator devs.

I also tried RtAudio but found it had bugs with its ALSA implementation.

Yuvi Masory