Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a noise on the PC speaker with Java

Tags:

I want to alert the user in a Swing application of certain events with an old fashioned PC Speaker beep (NOT the soundcard), since not on every PC there is a soundcard with an attached speaker, or volume might be turned to zero, or a headphone might be connected... 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.

like image 703
Hans-Peter Störr Avatar asked Nov 06 '08 18:11

Hans-Peter Störr


People also ask

How can I play Windows sounds in Java?

Play a Windows sound Tag(s): IO You can access a sound as defined in the Windows configuration for specific event. The JDK exposed them as properties and the values for these properties are retrieved with the getDesktopProperty() method on java. awt. Toolkit.


2 Answers

Toolkit.getDefaultToolkit().beep();

like image 101
Michael Myers Avatar answered Sep 18 '22 16:09

Michael Myers


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.

like image 38
Paulo Guedes Avatar answered Sep 21 '22 16:09

Paulo Guedes