Java相当于C#system.beep?

我正在研究一个Java程序,我真的需要能够以一定的频率和持续时间播放声音,类似于c#方法System.Beep,我知道如何在C#中使用它,但我找不到一种在Java中执行此操作的方法。 是否有一些等效或其他方式来做到这一点?

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); } } } 

我不认为有一种方法可以在便携式2 Java中使用“嘟嘟”来播放音乐1 。 你需要使用javax.sound.* API我认为……除非你能找到一个简化你的第三方库。

如果你想沿着这条路走下去,那么这个页面可能会给你一些想法。


1 – 除非你的用户都是聋哑人。 当然你可以做莫尔斯代码中的嘟嘟声……但这不是一个曲调。

2 – 显然,您可以对Windows蜂鸣function进行本机调用。 但那不可移植。

你可以用这个:

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

编辑

如果你试图播放任何持续时间和不同声音的东西,你应该真正研究Java MIDI库。 默认蜂鸣声无法满足您的需求,因为您无法更改蜂鸣声的长度。

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

只需打印出来:

 System.out.println("\007") 

至少在MacOS上有效。

我已经破解了一个适合我的function。 它使用来自javax.sound.sampled的一堆东西。 我已经准备好使用音频格式我的系统会自动从AudioSystem.getClip()提供一个新的Clip。 可能有各种各样的方法可以使其更加强大和高效。

 /** * Beeps. Currently half-assumes that the format the system expects is * "PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian" * I don't know what to do about the sample rate. Using 11025, since that * seems to be right, by testing against A440. I also can't figure out why * I had to *4 the duration. Also, there's up to about a 100 ms delay before * the sound starts playing. * @param freq * @param millis */ public static void beep(double freq, final double millis) { try { final Clip clip = AudioSystem.getClip(); AudioFormat af = clip.getFormat(); if (af.getSampleSizeInBits() != 16) { System.err.println("Weird sample size. Dunno what to do with it."); return; } //System.out.println("format " + af); int bytesPerFrame = af.getFrameSize(); double fps = 11025; int frames = (int)(fps * (millis / 1000)); frames *= 4; // No idea why it wasn't lasting as long as it should. byte[] data = new byte[frames * bytesPerFrame]; double freqFactor = (Math.PI / 2) * freq / fps; double ampFactor = (1 << af.getSampleSizeInBits()) - 1; for (int frame = 0; frame < frames; frame++) { short sample = (short)(0.5 * ampFactor * Math.sin(frame * freqFactor)); data[(frame * bytesPerFrame) + 0] = (byte)((sample >> (1 * 8)) & 0xFF); data[(frame * bytesPerFrame) + 1] = (byte)((sample >> (0 * 8)) & 0xFF); data[(frame * bytesPerFrame) + 2] = (byte)((sample >> (1 * 8)) & 0xFF); data[(frame * bytesPerFrame) + 3] = (byte)((sample >> (0 * 8)) & 0xFF); } clip.open(af, data, 0, data.length); // This is so Clip releases its data line when done. Otherwise at 32 clips it breaks. clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == Type.START) { Timer t = new Timer((int)millis + 1, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clip.close(); } }); t.setRepeats(false); t.start(); } } }); clip.start(); } catch (LineUnavailableException ex) { System.err.println(ex); } } 

编辑:显然有人改进了我的代码。 我还没试过,但试一试: https : //gist.github.com/jbzdak/61398b8ad795d22724dd

另一个解决方案,Windows依赖是使用JNA并直接调用Windows Beep函数 ,可在kernel32中使用。 不幸的是,JNA中的Kernel32在4.2.1中没有提供此方法,但您可以轻松扩展它。

 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); } 

要使用它:

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

如果您使用maven,则必须添加以下依赖项:

  net.java.dev.jna jna 4.2.1   net.java.dev.jna jna-platform 4.2.1  
 //Here's the full code that will DEFINITELY work: (can copy & paste) import java.awt.*; public class beeper { public static void main(String args[]) { Toolkit.getDefaultToolkit().beep(); } } 

您可以在此处获取Toolkit类的引用,其中定义了方法beep()