C语言 如何在 Windows 上用 C 发出 Beep 声音?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29493837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:50:06  来源:igfitidea点击:

How to make a Beep sound in C on Windows?

cwindowsaudio

提问by Ashish Ahuja

I am trying to make a program which includes a beep noise. I work on a 32 bit Windows Vista. I am using the Code::Blocks IDE which runs on a GNU compiler. My sample code is -

我正在尝试制作一个包含哔声的程序。我在 32 位 Windows Vista 上工作。我正在使用在 GNU 编译器上运行的 Code::Blocks IDE。我的示例代码是 -

#include <stdio.h>
#include <windows.h>
#include <dos.h>

int main(void)
{
    Beep(750, 300);
    printf("\n \n \t This is a dummy program for Beep.");
    getch();

    return 0;
}

On the Internet I read that we could also use \a in printf to make a beep. I tried that but it is not working. I checked my speakers and sound card. Everything is perfect but I hear no beep. Even the method I displayed in my sample code does not work.

在互联网上我读到我们也可以在 printf 中使用 \a 来发出哔哔声。我试过了,但它不起作用。我检查了我的扬声器和声卡。一切都很完美,但我没有听到哔哔声。即使我在示例代码中显示的方法也不起作用。

回答by Damian Yerrick

The C standard recommendsthat writing '\a'to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep()and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeepfunction, which plays a sound "asynchronously" (in the background while your program continues to run). The user can configure which sound is associated with each of these four values in the Sound control panel.

C 标准建议写入'\a'标准输出会产生可听或可见的警报信号,但如果重定向标准输出,它将不起作用。同样,一些较新的计算机缺少 WindowsBeep()和某些终端所依赖的 PC 蜂鸣器。要使 Windows PC 在桌面应用程序中播放警报声音,您可以调用特定于 Windows 的MessageBeep函数,该函数“异步”播放声音(在程序继续运行时在后台播放)。用户可以在“声音”控制面板中配置与这四个值中的每一个相关联的声音。

#include <windows.h>

/* Include one of these in a function */
MessageBeep(MB_OK);              /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION);    /* play question sound */
MessageBeep(MB_ICONWARNING);     /* play warning sound */

MessageBeep()is defined in User32.dll, so if this gives you link errors, make sure you're linking to the corresponding import library. In MinGW GCC (the compiler in Code::Blocks), add -lUser32to the list of libraries passed to the linker.

MessageBeep()在 中定义User32.dll,所以如果这给您链接错误,请确保您链接到相应的导入库。在 MinGW GCC(Code::Blocks 中的编译器)中,添加-lUser32到传递给链接器的库列表中。

回答by mcleod_ideafix

MessageBeep(-1);

From the MSDN documentation:

从 MSDN 文档:

MessageBeep function

Plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry.

BOOL WINAPI MessageBeep( _In_ UINT uType );... ...

Value for uType: 0xFFFFFFFF

Meaning: A simple beep. If the sound card is not available, the sound is generated using the speaker.

消息提示功能

播放波形声音。每种声音类型的波形声音由注册表中的一个条目标识。

BOOL WINAPI MessageBeep( _In_ UINT uType );……

价值uType0xFFFFFFFF

含义:简单的哔哔声。如果声卡不可用,则使用扬声器产生声音。



Also, and to my surprise, I've tested that. at least Windows 7 32 bits (and Windows Vista surely too) do some sort of emulation for the old 8253 I/O ports and the keyboard port, available to ring 3 processes, so the old implementation of sound()and nosound()should work. Unfornately, I haven't got any 32 bit machine available ATM so I cannot confirm this.

此外,令我惊讶的是,我已经测试过了。至少 Windows 7 32 位(当然还有 Windows Vista)对旧的 8253 I/O 端口和键盘端口进行某种模拟,可用于环 3 个进程,因此旧的实现sound()nosound()应该可以工作。不幸的是,我没有任何可用的 32 位机器 ATM,所以我无法确认这一点。

回答by Lundin

For the Beep()function in windows.h to actually work, you have to have a "PC speaker" buzzer in your PC, as stated in the function's documentation. So you need to have a fairly old PC and with Windows XP or older, since support for the function was apparently dropped in Windows Vista.

要使Beep()windows.h 中的函数真正起作用,您的 PC 中必须有一个“PC 扬声器”蜂鸣器,如函数文档中所述。因此,您需要有一台相当旧的 PC 并且使用 Windows XP 或更早版本,因为 Windows Vista 中显然已经放弃了对该功能的支持。

On newer Windows versions, calling Beep()gives a beep in the speakers instead, using your sound card. If you aren't getting any beep, it is possibly not related to the program, but perhaps to your specific computer hardware.

在较新的 Windows 版本上,呼叫Beep()会使用您的声卡在扬声器中发出哔哔声。如果您没有听到任何哔声,则可能与程序无关,而可能与您的特定计算机硬件有关。

回答by user8137222

Beep does work again in Windows since windows 7. The format is:

自 Windows 7 起,Beep 在 Windows 中再次起作用。格式为:

Beep(frequency, duration) where frequency is the pitch in hertz, and duration is the length in milliseconds

See https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx

请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx

回答by Mats Stenfeldt

This one works to on Windows 7 compiled with Visual Studio 2017. No problems with that.

这个适用于使用 Visual Studio 2017 编译的 Windows 7。没有问题。

printf("\n Bad request - check status code parameter\a");

回答by Mats Stenfeldt

You can use \a. At least it works in my computer.

您可以使用\a. 至少它适用于我的电脑。