I am working with a SMS handling, java based software and want to play a beep / alert sound whenever we receive a message. I tried looking at the java.sound libraries and could not find anything. I do not know if going the applet way of playing a sound will be okay in a java application! Are there any predefined sounds in any java libraries which we can call in an application? Any pointers will be appreciated!
5 Answers
If you just want to produce a beep or a quick alert, you can use the following code:
import java.awt.Toolkit;
Toolkit.getDefaultToolkit().beep();

- 2,465
- 5
- 13
- 26

- 7,597
- 1
- 24
- 26
-
4This might not work under Linux. – Augustin May 16 '16 at 18:35
-
2FYI, working fine on Windows 10. Thank you – Nabin Aug 25 '16 at 06:53
-
works fine on ubuntu linux – Pedro Siqueira Aug 20 '19 at 19:14
-
2not working on ubuntu 20 – withoutOne Nov 03 '20 at 06:54
You can generate your own sound if you looking for something less boring than a beep() without an external sound file.
import javax.sound.sampled.*;
public class SoundUtils {
public static float SAMPLE_RATE = 8000f;
public static void tone(int hz, int msecs)
throws LineUnavailableException
{
tone(hz, msecs, 1.0);
}
public static void tone(int hz, int msecs, double vol)
throws LineUnavailableException
{
byte[] buf = new byte[1];
AudioFormat af =
new AudioFormat(
SAMPLE_RATE, // sampleRate
8, // sampleSizeInBits
1, // channels
true, // signed
false); // bigEndian
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i < msecs*8; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
public static void main(String[] args) throws Exception {
SoundUtils.tone(1000,100);
Thread.sleep(1000);
SoundUtils.tone(100,1000);
Thread.sleep(1000);
SoundUtils.tone(5000,100);
Thread.sleep(1000);
SoundUtils.tone(400,500);
Thread.sleep(1000);
SoundUtils.tone(400,500, 0.2);
}
}

- 81,827
- 26
- 193
- 197

- 34,977
- 11
- 70
- 85
The applet route should be fine (and is very straightforward). To avoid creating an Applet instance you can use the static newAudioClip
method, and then call play()
on the AudioClip
created.
URL url = getClass().getResource("/foo/bar/sound.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
Here, the sound.wav
file is bundled in the jar
file in the foo/bar
package that you create. A fully functional class (where the wav
file is in the sounds
package) would look like this:
package sounds;
import java.applet.Applet;
import java.applet.AudioClip;
public class PlaySound {
public void PlayBeep() {
AudioClip clip = Applet.newAudioClip(getClass().getResource("/sounds/beep3.wav"));
clip.play();
}
}
Here, the path is given as /sounds/
because when you extract the jar
, you'll see that the wav
file is located in the first folder in the jar
, which is sounds
.
-
2any idea why this answer is not widely used? It seems extremely simple and useful... – RaKXeR Sep 20 '17 at 16:09
-
1
-
1
-
If you want to use the sound package to play an arbitrary sound file, you can use the javax.sound.sampled
package. Here is the code that will play a sound file:
private void playSound(File f) { Runnable r = new Runnable() { private File f; public void run() { playSoundInternal(this.f); } public Runnable setFile(File f) { this.f = f; return this; } }.setFile(f); new Thread(r).start(); } private void playSoundInternal(File f) { try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f); try { Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); try { clip.start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } clip.drain(); } finally { clip.close(); } } catch (LineUnavailableException e) { e.printStackTrace(); } finally { audioInputStream.close(); } } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

- 32,125
- 13
- 69
- 98
-
Can you explain why we need the thread to sleep after calling clip.start()? – lucks May 11 '11 at 01:19
-
It's been a while since I wrote this code, but my guess is that it would probably work just as well with a `Thread.yield()`. – Erick Robertson May 11 '11 at 17:05
-
I was referring to why we need to block in the first place, but I realized it's probably because we'd close the line before it finishes playing – lucks May 11 '11 at 19:42
-
I think you actually needed to pass back to system resources in order for the clip to actually start. – Erick Robertson May 12 '11 at 11:43
-
1