定时器到零时如何播放声音?

时间:2017-07-27 22:30:58

标签: c++ winapi timer while-loop

我正在尝试制作一个计时器并让它最后发出声音。我已经制作了计时器,它工作正常,但声音不会播放。这就是我到目前为止所做的:

int main() {
    //cout << "This is a timer. It is still in the making but it the seconds work properly." << endl;
    //Sleep(7000);
    //system("CLS");

    int input;

    cout << "Enter a time: ";
    cin >> input;
    cout << endl << "Begin." << endl;
    system("CLS");

    while (input != 0) {
        input--;
        cout << input << " seconds" << endl;
        Sleep(200);
        system("CLS");

        if (input == 0) {
            PlaySound(TEXT("C:\\Users\\iD Student\\Downloads\\Never_Gonna_Hit_Those_Notes.wav"), NULL, SND_FILENAME | SND_ASYNC);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

正如其他人所提到的那样,SND_ASYNC标志是罪魁祸首,你需要删除它。

我还建议您重新构建代码,以便将PlaySound()移到循环之外。在循环中多次检查input 0是没有意义的。循环结束后将调用循环后的代码:

const char* plural[] = {"", "s"};

int main()
{
    int input;

    cout << "Enter # of seconds: ";
    cin >> input;

    system("CLS");
    cout << "Begin." << endl;

    while (input > 0)
    {
        system("CLS");
        cout << input << " second" << plural[input != 1] << endl;
        Sleep(1000);
        --input;
    }

    system("CLS");
    cout << "Done." << endl;

    PlaySound(TEXT("C:\\Users\\iD Student\\Downloads\\Never_Gonna_Hit_Those_Notes.wav"), NULL, SND_FILENAME);

    return 0;
}

答案 1 :(得分:-1)

最后摆脱SND_ASYNC:

v = log(f0/f)/b;
相关问题