21

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.

Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
  • 2
    Just to note, I have a new PC that doesn't have an internal speaker. – Jonathan S. Nov 06 '08 at 18:09
  • @Jonathan S. - I didn't know such things existed. Is it a laptop? – Michael Myers Nov 06 '08 at 18:12
  • 1
    If you build your own, you can obviously just not connect the speaker, but I'm unaware of any that don't come with one. They're very necessary for debugging motherboard problems (beep codes), among other things. – rmeador Nov 06 '08 at 18:24
  • No, it's a gaming PC that I built. I think it could have a speaker if I hooked one up. But the case I used didn't have one and there isn't one on the motherboard. I wouldn't be surprised if this becomes more common as a cost-savings measure, but I did miss those POST beeps for the first few days... – Jonathan S. Nov 06 '08 at 18:24
  • https://stackoverflow.com/a/1932537/3651739 checkout this answer – Jishnu Prathap Oct 27 '17 at 06:21

6 Answers6

22

Toolkit.getDefaultToolkit().beep();

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
8

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
  • 7,189
  • 5
  • 40
  • 60
4

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

Community
  • 1
  • 1
Jakob Cosoroaba
  • 1,702
  • 4
  • 24
  • 34
3

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
  • 21,187
  • 12
  • 85
  • 133
  • 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. ;-) – Dr. Hans-Peter Störr Oct 25 '10 at 08:37
2

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

Elie
  • 13,693
  • 23
  • 74
  • 128
  • 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 Nov 06 '08 at 18:14
0
    {
     If (whatever you named the file) = true
     Then
     Process.Start ("C:\Windows\Media\{whatever you named the file})
     }

I use that in C#. It's late for me the code is not exactly accurate before people bash this post. Put a write directory to name a folder and declare it the default location. Move the sound of your choice to this folder and it will play the audio tone. Set the timer in Java to loop the sound every 2 seconds to get the persons attention. Hope this helps as I'm learning my self!

  • You should at least mention that that's a Windows-only answer, not a general Java answer. – Toby Speight Nov 08 '16 at 13:49
  • 1
    The problem of this is also that it would trigger a noise just bia the sound card, which is what I was trying to avoid since that could be connected to a headphone, have the volume turned to zero and whatnot. – Dr. Hans-Peter Störr Jul 12 '19 at 09:12