简体   繁体   中英

Why doesn't Beep function work on Windows 10

I'm having a problem with the Beep function programming in C with Dev C++.

I need a beep lasting for 25 ms and the code line I'm using is

Beep (1000, 25)

This works fine on Windows 8/8.1, I'm getting exactly what I want. But when I run the same program on Windows 10, it does not beep! I tried to rise up the duration, and then I noticed that if it is 100ms or greater, it beeps...

Any ideas of what's going on?
Could this may be caused by something related to the hardware?

I don't think it's a limitation of Windows itself, I have this program which appears to work fine when given the arguments you describe in your question (although 25ms is a very short blip, it's still audible).

Granted it's compiled under Visual Studio 2019 but I don't think that would make a difference.

#include <iostream>
#include <cstdlib>
#include <Windows.h>

int errorExit(char *str) {
    std::cerr << str << std::endl;
    return 1;
}

int main(int argc, char *argv[]) {
    if (argc != 3)
        return errorExit("beep: Usage: beep <frequency> <duration-in-millisecs>");

    int freq = std::atoi(argv[1]);
    if ((freq < 37) || (freq > 32767))
        return errorExit("beep: Frequency must be 37-32767 inclusive");

    int duration = atoi(argv[2]);
    if ((duration < 1) || (duration > 10000))
        return errorExit("beep: Duration must be 1-10000 inclusive");

    Beep(freq, duration);

    return 0;
}

If you can't get it working at the frequencies and durations you need, you may want to look into something like PlaySound , which can use arbitrary audio files, not just simple beeps. Then put your simple beeps into an audio file (using Audacity or some other tool) and play them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM