Java 中的系统声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3927941/
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
System Sounds in Java
提问by Supuhstar
I'm trying to code an error dialog, and I want it to call the proper system-specified sound. Is there any way to access system sounds from Java (i.e. Startup sound, default beep, asterisk, critical stop, etc.)?
我正在尝试编写一个错误对话框,我希望它调用正确的系统指定声音。有没有办法从 Java 访问系统声音(即启动声音、默认哔声、星号、紧急停止等)?
Note:I know about java.awt.Toolkit.getDefaultToolkit().beep();
注:我知道java.awt.Toolkit.getDefaultToolkit().beep();
回答by MyPasswordIsLasercats
Here ya go (exclusively for windows:)
给你(仅适用于窗户:)
final Runnable runnable =
(Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.exclamation");
if (runnable != null) runnable.run();
More sounds for Windows (all pages contain the same content): Java 6, Java 7, Java 8. (Good luck finding some for other OS!)
Windows 的更多声音(所有页面都包含相同的内容):Java 6、Java 7、Java 8。(祝你为其他操作系统找到一些好运!)
回答by Gray
I assume you are talking about windows system sounds? My mac doesn't have a "critical stop" noise. ;-)
我假设您在谈论 Windows 系统声音?我的 Mac 没有“关键停止”噪音。;-)
You'll need to find the proper filesystem path to those sound files. I assume they are wav files so something like this should work:
您需要找到这些声音文件的正确文件系统路径。我假设它们是 wav 文件,所以这样的东西应该可以工作:
new JavaSoundAudioClip(new FileInputStream(new File("/tmp/go.wav"))).play();
The file may have a path such as:
该文件可能有一个路径,例如:
C:\WINDOWS\MEDIA\Microsoft Office 2000\EXPLODE.WAV
NOTE: This will return immediately although the sound has been "queued" to the audio device. You can call stop()
if you need to stop it.
注意:尽管声音已“排队”到音频设备,但这将立即返回。stop()
如果你需要阻止它,你可以打电话。
If you need to do something more special take a look at this Java forum. Here's some documentation which breaks down how to use the audio system more directly.