6

Hi I am trying to write an application that will play morse code. I just wanted to know if there was a default system sound in java like some beep, or do I have to download some sound file from the internet?

user69514
  • 26,935
  • 59
  • 154
  • 188

3 Answers3

7

You can:

Print the ASCII bell character to the console (will emit a beep sound):

public class DoBeep {
    public static main(String args[]) {
        System.out.print("\007"); // bell ASCII char
        System.out.flush();
    }
}

Use the beep() method that will use the buzzer on the motherboard:

import java.awt.*;

public class DoBeep {
    public static void main(String args[]) {
        Toolkit.getDefaultToolkit().beep();     
    }
}
rogeriopvl
  • 51,659
  • 8
  • 55
  • 58
  • 1
    What is the benefit of system.out.flush(); ? – Sven van den Boogaart Oct 21 '13 at 16:16
  • 1
    @SvenvandenBoogaart See the Javadoc for [`PrintStream#flush`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/PrintStream.html#flush()). The output bytes may be buffered rather than sent immediately to the underlying output stream. Calling `flush` forces any buffered content to be sent immediately, hence the metaphorical method name “flush”. – Basil Bourque Dec 30 '22 at 06:38
2

Have a look at the Java Sound API, which can play MIDI tones.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
1

You may want to look at jMorse for some tips. This isn't to discourage you from your effort, but rather to provide a reference.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107