Visual C++ 或 C 播放声音 [英] Visual C++ or C play sounds
本文介绍了Visual C++ 或 C 播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个用 C (Visual C++) 编写的简单游戏我想知道是否有播放声音的方法,谢谢(我使用的是 Visual Studio)
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)
推荐答案
查看PlaySound() 函数.
如果您使用 SND_ASYNC
标志调用 PlaySound()
,该函数会在声音开始后立即返回.
If you call PlaySound()
with the SND_ASYNC
flag, the function returns immediately after beginning the sound.
例如:
#include <windows.h>
#include <mmsystem.h>
PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME);
您还必须在项目设置中添加 Winmm.lib
.
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;
}
在 stdafx.h 中:
and in stdafx.h:
#pragma once
#include <iostream>
#include <tchar.h>
#include <conio.h>
这篇关于Visual C++ 或 C 播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文