tags:

views:

1890

answers:

6

I want to alert the user in a Swing application of certain events with an old fashioned PC Speaker beep, since not on every PC there is a soundcard with an attached speaker. (At least on my work PC there is no speaker, with a reason. :-) How can I do this?

UPDATE: java.awt.Toolkit.getDefaultToolkit().beep() seems usually to generate a sound on the soundcard. It only uses the speaker if there is no active soundcard. To print an ASCII value 7 works only if the application is launched in a terminal, which at least a Swing app usually isn't. So the question is still open.

+8  A: 

Toolkit.getDefaultToolkit().beep();

Michael Myers
+2  A: 

ASCII value 7 is a beep. So just print that character.

Elie
This may or may not work depending upon how the application is launched. If it is launched apart from a normal terminal, the print will not be interpreted by the shell. Mmyers solution is more flexible.
James Van Huis
+4  A: 

Try this:

java.awt.Toolkit.getDefaultToolkit().beep();

It worked for me, although I'm not sure whether this was the PC Speaker beep or some OS-generated beep.

Paulo Guedes
Works on OS X, too. It plays your system alert sound.
Kevin Conner
I'm pretty sure that on Windows, if you don't have a soundcard/soundcard driver installed and something happens that requires that kind of beep, the system speaker beeps instead.
Daniel I-S
A: 

I read somewhere that you need to use a C/C++ dll and connect it with JNI, to make that work,.. link is offline

Ok I found what you're looking for

http://pyx4j.com/pyx4j-native/index.html

he pyx4j-native project is a collection of java wrappers for windows functions like time and beep. Now only works on windows.

NativeThreadDump - Send CtrlBreak Event to current process

Beep - Make a sound using PC speaker

FileUtil - Access and modify file creation time. Used in com.pyx4j.log.RollingFileAppender

NativeTimer - System high-resolution performance counter used before Java 5

Midday
A: 

Press shift 5 times or click on command prompt and right click "mark" then press space bar and whalaah! BEEP BEEP. Happy getting in trouble at school :]

Lee
A: 

Other than the beep sound you can try JFugue.

JFugue is an open-source Java API for programming music without the complexities of MIDI.

Chankey Pathak
Thanks for the answer, but is JFugue able to make a sound on the PC Speaker even if a Soundcard is present? If not, this does not answer the question. ;-)
hstoerr