Visual C++ or C play sounds

痞子三分冷 提交于 2021-02-08 09:01:25

问题


I'm developping an easy game written in C (Visual C++) and I want to know if there is a way to play sounds, thanks (I'm using Visual Studio)


回答1:


Take a look at the PlaySound() function.

If you call PlaySound() with the SND_ASYNC flag, the function returns immediately after beginning the sound.

For example:

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

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

You'll also have to add Winmm.lib in your project settings.


Here's a quick example that should work:

#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;
}

and in stdafx.h:

#pragma once

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


来源:https://stackoverflow.com/questions/42819725/visual-c-or-c-play-sounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!