Visual C++ 或 C 播放声音

Visual C++ or C play sounds

我正在开发一款用 C (Visual C++) 编写的简单游戏 我想知道是否有办法播放声音, 谢谢 (我正在使用 Visual Studio)

看看 PlaySound() 函数。

如果您使用 SND_ASYNC 标志调用 PlaySound(),函数 returns 会在开始播放声音后立即执行。

例如:

#include <windows.h>
#include <mmsystem.h>

PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME);

您还必须在项目设置中添加 Winmm.lib


这是一个应该有效的简单示例:

#pragma once

#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>

int _tmain(int argc, _TCHAR* argv[])
{
    if(!PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME))
        printf("error\n");
    else
        printf("ok\n");

    getch();
    return 0;
}

并在 stdafx.h 中:

#pragma once

#include <iostream>
#include <tchar.h>
#include <conio.h>