c# - 如何使用PC扬声器发出哔哔声?

标签 c# hardware beep

我想在 C# 中使用 PC 扬声器发出哔声。使用以下代码时:

[DllImport("kernel32.dll", EntryPoint = "Beep", SetLastError = true,
ExactSpelling = true)]
public static extern bool Beep(uint frequency, uint duration);

static void Main()
{
    while (true)
    {
        Beep(1000, 500);
        Thread.Sleep(2000);
    }
}

它不是通过 PC 扬声器发出哔哔声,而是简单地将给定频率和持续时间的声音输出到默认声音设备(例如耳机)。使用 Console.Beep() 时也会发生同样的事情。

为什么?

注意事项:

  • PC 扬声器已打开。当我启动电脑时,它会发出哔哔声。

  • 操作系统为 Windows 8。

最佳答案

从 Windows 7 开始,您无法再通过内置扬声器轻松发出声音。

For Windows 7, we resolved the issue completely – we moved all the functionality that used to be contained in Beep.Sys into the user mode system sounds agent – now when you call the Beep() API instead of manipulating the 8254 chip the call is re-routed into a user mode agent which actually plays the sounds.

[…]

There were also some unexpected consequences. The biggest was that people started noticing when applications called Beep(). They had placed their PCs far enough away (or there was enough ambient noise) that they had never noticed when their PC was beeping at them until the sounds started coming out their speakers.

详情请看这里

https://learn.microsoft.com/en-us/archive/blogs/larryosterman/whats-up-with-the-beep-driver-in-windows-7

关于c# - 如何使用PC扬声器发出哔哔声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794846/

相关文章:

c# - 为什么 IEnumerable<T> 在 C# 4 中是协变的?

c# - WebApi OData 内联计数不起作用

c# - 捕获没有焦点的击键

java - 使用C/C++/Java获取基本硬件信息

python - RPI dht22 带 flask : Unable to set line 4 to input - Timed out waiting for PulseIn message

C# Windows 应用程序阻止 Windows 关闭/注销

android - 如何在 sleep 模式下接收硬件按键事件?

c - 在 C、MacOS 中播放提示音

api - 使用Mozilla音频API创建提示音

C++ WIN32 : where to put code that executes as program is running