How to PlaySound in C++ using Windows API?(如何使用 Windows API 在 C++ 中播放声音?)
问题描述
我尝试在我的编码中播放音乐文件,但失败了.我的音乐文件在保存 .cpp 文件的同一个文件夹中.
I try to play a music file in my coding, but failed. I have my music file in the same folder which save .cpp file.
有人可以帮我吗?
我的代码是:
#include <iostream>
#include <windows.h>
int main() {
PlaySound("kenny g.WAV", NULL, SND_ASYNC);
}
推荐答案
您需要使用绝对路径,确保您发送的是文件名(使用 SND_FILENAME 标志),并暂停程序足够长的时间以播放声音文件(例如,使用 getchar()).您需要在项目设置中链接 winmm.lib 库,并在标题中链接 #include windows.h 和 mmsystem.h.
You need to use the absolute path, make sure that you're sending a filename (use SND_FILENAME flag), and pause the program long enough to play the sound file (e.g., use getchar()). You need to link the winmm.lib library in your project settings, and #include windows.h and mmsystem.h in the header.
#include <windows.h>
#include <mmsystem.h>
int main() {
PlaySoundA((LPCSTR) "C:\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
getchar();
}
API:http://msdn.microsoft.com/en-us/library/ms712879(VS.85).aspx
应该是这样.让我知道,谢谢!
API: http://msdn.microsoft.com/en-us/library/ms712879(VS.85).aspx
That should be it. Let me know, thanks!
这篇关于如何使用 Windows API 在 C++ 中播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Windows API 在 C++ 中播放声音?
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
