如何用C#让电脑发出蜂鸣声?

发布于 2024-07-10 02:54:11 字数 42 浏览 7 评论 0原文

如何在没有外部扬声器的情况下用 C# 使计算机的内部扬声器发出蜂鸣声?

How do I make the computer's internal speaker beep in C# without external speakers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时间海 2024-07-17 02:54:11

在 .Net 2.0 中,您可以使用 Console.Beep< /代码>

// Default beep
Console.Beep();

您还可以指定蜂鸣声的频率和长度(以毫秒为单位)。

// Beep at 5000 Hz for 1 second
Console.Beep(5000, 1000);

In .Net 2.0, you can use 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);
铁憨憨 2024-07-17 02:54:11

使用 System.Media.SystemSounds 获取各种事件的声音,然后播放< /a> 他们:

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

Use System.Media.SystemSounds to get the sounds for various events, then Play them:

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();
绝不放开 2024-07-17 02:54:11

解决方案是,

Console.Beep

The solution would be,

Console.Beep
九公里浅绿 2024-07-17 02:54:11

打印 响铃字符(ASCII 代码 7)。 您可以使用警报/警报 1 中的转义序列 \a

Console.WriteLine("\a")            

1 \b 用于退格

Print the bell character (ASCII code 7). You can use the escape sequence \a from alert/alarm 1.

Console.WriteLine("\a")            

1 \b is for backspace

狠疯拽 2024-07-17 02:54:11

已确认Windows 7 及更高版本(至少 64 位或两者)不使用系统扬声器,而是将呼叫路由到默认声音设备。

因此,在 win7/8/10 中使用 system.beep() 不会使用内部系统扬声器发出声音。 相反,如果外部扬声器可用,您会听到蜂鸣声。

It is confirmed that Windows 7 and newer versions (at least 64bit or both) do not use system speaker and instead they route the call to the default sound device.

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.

大海や 2024-07-17 02:54:11

我刚刚在为自己寻找解决方案时遇到了这个问题。
您可能会考虑通过运行一些 kernel32 的东西来调用系统蜂鸣声函数。

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
        }

这与运行 powershell 相同:

[console]::beep(1000, 1600)
[console]::beep(2000, 400)

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.

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:

[console]::beep(1000, 1600)
[console]::beep(2000, 400)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文