问题
In Java, is there any functions that can emit a (motherboard) beep with a given frequency and duration? In many languages I've worked with, you can; Beeps are useful for indicating certain types of errors, also for easily generating music.
The standard way to beep would be either print the BEEP character (\007 or something) or to use Toolkit.beep(), neither of which allows you to specify frequency and duration.
I'm thinking of implementing a native method, but there should be a library somewhere to do this simple task. I'm looking for something like SomeClass.beep(500,1000);
My application will only be supported on Windows.
Thanks.
回答1:
See this link here:
http://mindprod.com/products2.html#SPEAKER
It contains Java source and a DLL for achieving the functionality you require. It emits various sounds from the internal PC speaker at a specific frequency for a given duration. Check out the Auld Lang Syne example : ) It uses JNI to achieve this with the included DLL.
btw.. I don't believe using the toolkit is reliable. It didn't produce output on my PC.
回答2:
If you are 100% sure that you will support only windows, you can always call the corresponding os method using JNA
回答3:
The library algo4j
can also do that.
https://github.com/ice1000/algo4j/
See this file:
https://github.com/ice1000/algo4j/blob/master/src/test/kotlin/org/algo4j/win/WinAPITest.kt
来源:https://stackoverflow.com/questions/691743/beep-library-for-java