如何在 C# 中使计算机发出哔哔声?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321135/
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 can I make the computer beep in C#?
提问by a_hardin
How do I make the computer's internal speaker beep in C# without external speakers?
如何在没有外部扬声器的情况下在 C# 中使计算机的内部扬声器发出哔哔声?
采纳答案by a_hardin
In .Net 2.0, you can use Console.Beep().
在 .Net 2.0 中,您可以使用 Console.Beep()。
// Default beep
Console.Beep();
You can also specify the frequency and length of the beep in milliseconds.
您还可以以毫秒为单位指定蜂鸣声的频率和长度。
// Beep at 5000 Hz for 1 second
Console.Beep(5000, 1000);
For more information refer http://msdn.microsoft.com/en-us/library/8hftfeyw%28v=vs.110%29.aspx
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/8hftfeyw%28v=vs.110%29.aspx
回答by Barry Kelly
The solution would be,
解决办法是,
Console.Beep
回答by Chris Ballance
Try this
尝试这个
Console.WriteLine("\a")
Console.WriteLine("\a")
回答by Ta01
You can also use the relatively unused:
您还可以使用相对未使用的:
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Question.Play();
System.Media.SystemSounds.Hand.Play();
Documentation for this sounds is available in http://msdn.microsoft.com/en-us/library/system.media.systemsounds(v=vs.110).aspx
此声音的文档可在http://msdn.microsoft.com/en-us/library/system.media.systemsounds(v=vs.110).aspx 中找到
回答by kuma DK
It is confirmed that Windows 7 and newer versions(at least 64bit or both) do not use system speakerand instead they route the call to the default sound device.
已确认Windows 7 和更新版本(至少 64 位或两者)不使用系统扬声器,而是将呼叫路由到默认声音设备。
So, using system.beep()
in win7/8/10 will not produce sound using internal system speaker. Instead, you'll get a beep sound from external speakers if they are available.
所以,system.beep()
在win7/8/10下使用系统内置扬声器不会产生声音。相反,如果外部扬声器可用,您会从外部扬声器听到蜂鸣声。
回答by Jakub Szumiato
I just came across this question while searching for the solution for myself. You might consider calling the system beep function by running some kernel32 stuff.
我刚刚在为自己寻找解决方案时遇到了这个问题。您可能会考虑通过运行一些 kernel32 内容来调用系统 beep 函数。
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
public static extern bool Beep(int freq, int duration);
public static void TestBeeps()
{
Beep(1000, 1600); //low frequency, longer sound
Beep(2000, 400); //high frequency, short sound
}
This is the same as you would run powershell:
这与您运行 powershell 相同:
[console]::beep(1000, 1600)
[console]::beep(2000, 400)