Java equivalent of C# system.beep?

后端 未结 8 1286
半阙折子戏
半阙折子戏 2021-01-01 10:31

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 i

相关标签:
8条回答
  • 2021-01-01 10:42

    Just print it:

    System.out.println("\007")
    

    Works on Windows and MacOS.

    0 讨论(0)
  • 2021-01-01 10:43

    Another solution, Windows dependent is to use JNA and call directly Windows Beep function, available in kernel32. Unfortunately Kernel32 in JNA doesn't provide this method in 4.2.1, but you can easily extend it.

    public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
    
            /**
             * Generates simple tones on the speaker. The function is synchronous; 
             * it performs an alertable wait and does not return control to its caller until the sound finishes.
             * 
             * @param dwFreq : The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
             * @param dwDuration : The duration of the sound, in milliseconds.
             */
            public abstract void Beep(int dwFreq, int dwDuration);
    }
    

    To use it :

    static public void main(String... args) throws Exception {
        Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
        kernel32.Beep(800, 3000);
    }
    

    If you use maven you have to add the following dependencies :

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>4.2.1</version>
    </dependency>
    
    0 讨论(0)
  • 2021-01-01 10:45

    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

    0 讨论(0)
  • 2021-01-01 10:52

    You can get the reference of Toolkit class here, where the method beep() is defined.

    0 讨论(0)
  • 2021-01-01 10:52

    Use Applet instead. You'll have to supply the beep audio file as a wav file, but it works. I tried this on Ubuntu:

    package javaapplication2;
    
    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class JavaApplication2 {
    
        public static void main(String[] args) throws MalformedURLException {
            File file = new File("/path/to/your/sounds/beep3.wav");
            URL url = null;
            if (file.canRead()) {url = file.toURI().toURL();}
            System.out.println(url);
            AudioClip clip = Applet.newAudioClip(url);
            clip.play();
            System.out.println("should've played by now");
        }
    }
    //beep3.wav was available from: http://www.pacdv.com/sounds/interface_sound_effects/beep-3.wav
    
    0 讨论(0)
  • 2021-01-01 10:54

    If you're using SWT widgets, you can do it this way (SYSTEM SOUND BEEP!)

    org.eclipse.swt.widgets.Display.getCurrent().beep();
    

    if you want NATIVE JAVA, here is a class for it: https://github.com/marcolopes/dma/blob/master/org.dma.java/src/org/dma/java/util/SoundUtils.java

    0 讨论(0)
提交回复
热议问题