Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliably playing a short sound in Java

Tags:

java

audio

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/generic.wav for those of you using Ubuntu.)

The problem is, I can't seem to figure out how to play that sample reliably, i.e., in all my attempts, I can get my program to play the sound in 4 out of 5 times or so, but never 100%.

This is what has worked best so far as a stand-alone program:

File soundFile = new File("/usr/share/sounds/generic.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);
clip.start(); 

(Note that the code doesn't even call clip.stop()) But even with that one, if I run it a couple of times in a row, sooner or later there will be a run without any sound being played, but no Exceptions either.

Variations I've tried:

1) Loading the audio file into a byte array and passing that to clip.open

2) Attaching a LineListener to the clip to wait for STOP events

plus a couple of random try-outs, but so far I haven't managed to create code that works every time.

I'm also aware of the following bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4434125 but I'm using Java 1.6 and the report claims that things should be fine with Java 1.5 or later.

Any ideas? Is it PulseAudio?

like image 776
Thomas Avatar asked Feb 25 '10 16:02

Thomas


People also ask

Can you play sounds in Java?

The Java Sound APIs are designed to play sounds smoothly and continuously, even very long sounds. As part of this tutorial, we'll play an audio file using Clip and SourceDataLine Sound APIs provided by Java. We'll also play different audio format files.

What is AudioSystem in Java?

The AudioSystem class acts as the entry point to the sampled-audio system resources. This class lets you query and access the mixers that are installed on the system. AudioSystem includes a number of methods for converting audio data between different formats, and for translating between audio files and streams.


2 Answers

I suspect now that the reason my test program failed was a timing issue. Either I attempted playing the short sound before the samples were fully loaded, or the program terminated too quickly. The reason for this suspicion is that if I change the above code slightly like so:

File soundFile = new File("/usr/share/sounds/generic.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);

while (System.in.read() == '\n') {
    clip.stop();
    clip.setFramePosition(0);
    clip.start();
}

then the short sound gets played correctly every time I hit enter.

like image 33
Thomas Avatar answered Sep 30 '22 14:09

Thomas


I've had great luck with the BASS Audio Library.

It's written natively, so it breaks write-once, run-anywhere, but it will work on Windows, OS/X, and Linux, which is anywhere enough for my needs.

like image 187
Dean J Avatar answered Sep 30 '22 13:09

Dean J