Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Sounds in Java

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();

like image 665
Ky. Avatar asked Oct 13 '10 20:10

Ky.


People also ask

Can you play sounds in Java?

Java API Support for MP3 Format. Currently, both Clip and SourceDataLine can play audio files in AIFC, AIFF, AU, SND, and WAV formats. We can check the supported audio format using AudioSystem: Type[] list = AudioSystem.

What is Java Sound API?

"The Java Sound API is a low-level API for effecting and controlling input. and output of audio media. It provides explicit control over the capabilities. commonly required for audio input and output in a framework that promotes. extensibility and flexibility."


2 Answers

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!)

like image 78
MyPasswordIsLasercats Avatar answered Oct 04 '22 06:10

MyPasswordIsLasercats


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.

like image 36
Gray Avatar answered Oct 04 '22 04:10

Gray