如何使用 Java 在 PC 扬声器上发出噪音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/269657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a noise on the PC speaker with Java
提问by Hans-Peter St?rr
I want to alert the user in a Swing application of certain events with an old fashioned PC Speaker beep (NOT the soundcard), since not on every PC there is a soundcard with an attached speaker, or volume might be turned to zero, or a headphone might be connected... How can I do this?
我想在 Swing 应用程序中用老式的 PC 扬声器蜂鸣声(不是声卡)提醒用户某些事件,因为不是每台 PC 上都有一个带有扬声器的声卡,或者音量可能会变成零,或者耳机可能已连接...我该怎么做?
UPDATE: java.awt.Toolkit.getDefaultToolkit().beep() seems usually to generate a sound on the soundcard. It only uses the speaker if there is no active soundcard. To print an ASCII value 7 works only if the application is launched in a terminal, which at least a Swing app usually isn't. So the question is still open.
更新: java.awt.Toolkit.getDefaultToolkit().beep() 似乎通常会在声卡上产生声音。如果没有活动声卡,它仅使用扬声器。打印 ASCII 值 7 仅在应用程序在终端中启动时才有效,至少 Swing 应用程序通常不是这样。所以这个问题仍然悬而未决。
回答by Michael Myers
Toolkit.getDefaultToolkit().beep();
Toolkit.getDefaultToolkit().beep();
回答by Elie
ASCII value 7 is a beep. So just print that character.
ASCII 值 7 是哔哔声。所以只需打印该字符。
回答by Paulo Guedes
Try this:
尝试这个:
java.awt.Toolkit.getDefaultToolkit().beep();
It worked for me, although I'm not sure whether this was the PC Speaker beep or some OS-generated beep.
它对我有用,尽管我不确定这是 PC 扬声器的蜂鸣声还是某些操作系统生成的蜂鸣声。
回答by Jakob Cosoroaba
I read somewhere that you need to use a C/C++ dll and connect it with JNI, to make that work,.. link is offline
我在某处读到您需要使用 C/C++ dll 并将其与 JNI 连接,以使其工作,.. 链接处于脱机状态
Ok I found what you're looking for
好的,我找到了你要找的东西
http://pyx4j.com/pyx4j-native/index.html
http://pyx4j.com/pyx4j-native/index.html
he pyx4j-nativeproject is a collection of java wrappers for windows functions like time and beep. Now only works on windows.
NativeThreadDump - Send CtrlBreak Event to current process
Beep - Make a sound using PC speaker
FileUtil - Access and modify file creation time. Used in com.pyx4j.log.RollingFileAppender
NativeTimer - System high-resolution performance counter used before Java 5
他pyx4j本地项目是Java包装的像时间和窗口功能的集合蜂鸣声。现在只适用于 Windows。
NativeThreadDump - 将 CtrlBreak 事件发送到当前进程
Beep - 使用 PC 扬声器发出声音
FileUtil - 访问和修改文件创建时间。在 com.pyx4j.log.RollingFileAppender 中使用
NativeTimer - Java 5 之前使用的系统高分辨率性能计数器
回答by Chankey Pathak
回答by Beginner coder
{
If (whatever you named the file) = true
Then
Process.Start ("C:\Windows\Media\{whatever you named the file})
}
I use that in C#. It's late for me the code is not exactly accurate before people bash this post. Put a write directory to name a folder and declare it the default location. Move the sound of your choice to this folder and it will play the audio tone. Set the timer in Java to loop the sound every 2 seconds to get the persons attention. Hope this helps as I'm learning my self!
我在 C# 中使用它。在人们抨击这篇文章之前,代码对我来说已经晚了。放置一个写入目录来命名文件夹并将其声明为默认位置。将您选择的声音移动到此文件夹,它将播放音频。在 Java 中设置计时器以每 2 秒循环一次声音以引起人们的注意。希望这对我学习自我有所帮助!