Emit a beepTag(s): IO


There are three ways to emit a beep in Java.
  • Use an AU audio file
    For Java, AU files need to be created at a sample rate of 8000. Any sample rate beyound that will not work. See also this How-to.

  • Print the ASCII Bell character to the console
    public class TestBeep {
      public static main(String args[]) {
         // ASCII bell
         System.out.print("\007");
         System.out.flush();
      }
    }
    
    The buzzer on the motherboard is used.
  • Starting with JDK 1.1, use the beep method in the Toolkit
    import java.awt.*;
    public class Beep {
      public static void main(String args[]) {
         Toolkit.getDefaultToolkit().beep();     
      }
    }
    
    (Windows) The sound used is determined from the setting found in Control Panel/Sounds and Devices/Sounds/Sound Scheme/"Default Beep". If no sound file is selected then the beep() will be a silence.
    blog comments powered by Disqus