18

I'm trying to code an error dialog, and I want it to call the proper system-specified sound. Is there any way to access system sounds from Java (i.e. Startup sound, default beep, asterisk, critical stop, etc.)?

Note: I know about java.awt.Toolkit.getDefaultToolkit().beep();

2
  • Are you meaning to play tunes with the PC Speaker? Or just cause a beep? I don't know how you play tunes but I sure would like to know.
    – Steven
    Commented Oct 13, 2010 at 23:05
  • I know how to play sound files, I want to know how to access an OS's default interface sounds
    – Ky -
    Commented Oct 16, 2010 at 10:51

2 Answers 2

29

Here ya go (exclusively for windows:)

final Runnable runnable =
     (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.exclamation");
if (runnable != null) runnable.run();

More sounds for Windows (all pages contain the same content): Java 6, Java 7, Java 8. (Good luck finding some for other OS!)

3

I assume you are talking about windows system sounds? My mac doesn't have a "critical stop" noise. ;-)

You'll need to find the proper filesystem path to those sound files. I assume they are wav files so something like this should work:

new JavaSoundAudioClip(new FileInputStream(new File("/tmp/go.wav"))).play();

The file may have a path such as:

C:\WINDOWS\MEDIA\Microsoft Office 2000\EXPLODE.WAV

NOTE: This will return immediately although the sound has been "queued" to the audio device. You can call stop() if you need to stop it.

If you need to do something more special take a look at this Java forum. Here's some documentation which breaks down how to use the audio system more directly.

2
  • Oh, then find the sound files somewhere and include them in your jar/war. Each operating systems has vastly different sounds (or none) and Java does not, as far as I know, provide a standard mechanism for triggering them.
    – Gray
    Commented Oct 18, 2010 at 13:14
  • @Supuhstar Me too, maybe I will supply my own sound effects.
    – jamie
    Commented Apr 12, 2017 at 0:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.