Java equivalent of C# system.beep?

Glen654 picture Glen654 · May 27, 2012 · Viewed 37.6k times · Source

I am working on a Java program, and I really need to be able to play a sound by a certain frequency and duration, similarly to the c# method System.Beep, I know how to use it in C#, but I can't find a way to do this in Java. Is there some equivalent, or another way to do this?

using System;

class Program
{
    static void Main()
    {
    // The official music of Dot Net Perls.
    for (int i = 37; i <= 32767; i += 200)
    {
        Console.Beep(i, 100);
    }
    }
}

Answer

Hunter McMillen picture Hunter McMillen · May 27, 2012

You can use this:

java.awt.Toolkit.getDefaultToolkit().beep();

EDIT

If you are trying to play anything of duration and with different sounds you should really look into the Java MIDI library. The default beep won't be able to meet your needs as you can't change the length of the beep.

http://www.oracle.com/technetwork/java/index-139508.html