如何使用 Windows API 在 C++ 中播放声音?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1565439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:26:43  来源:igfitidea点击:

How to PlaySound in C++ using Windows API?

c++winapiplaysound

提问by

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.

我尝试在我的编码中播放音乐文件,但失败了。我的音乐文件在保存 .cpp 文件的同一个文件夹中。

Can someone help me?

有人能帮我吗?

My code is:

我的代码是:

#include <iostream>  
#include <windows.h>

int main() { 
    PlaySound("kenny g.WAV", NULL, SND_ASYNC);    
}

回答by Luis B

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.

您需要使用绝对路径,确保您发送的是文件名(使用 SND_FILENAME 标志),并暂停程序足够长的时间来播放声音文件(例如,使用 getchar())。您需要在项目设置中链接 winmm.lib 库,并在标题中链接 #include windows.h 和 mmsystem.h。

#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
That should be it. Let me know, thanks!

API:http: //msdn.microsoft.com/en-us/library/ms712879(VS.85).aspx
应该是这样。让我知道,谢谢!

回答by Cool Guy 2131

try adding -lwinmm to your compiler settings thingy. It worked for me. Just type that in the compiler options area and it will work.

尝试将 -lwinmm 添加到您的编译器设置中。它对我有用。只需在编译器选项区域中输入它,它就会起作用。

回答by Nick Dandoulakis

int main() { 
    PlaySound("kenny g.WAV", NULL, SND_ASYNC); 
}

With the SND_ASYNCflag your program can (and it will) terminate immediatelly!

使用该SND_ASYNC标志,您的程序可以(并且会)立即终止!

Try PlaySound("kenny g.WAV", NULL, SND_SYNC);first to see if it works.

PlaySound("kenny g.WAV", NULL, SND_SYNC);首先尝试看看它是否有效。

回答by aJ.

Can you use absolute path and check if it is path error?

您可以使用绝对路径并检查它是否是路径错误吗?

Ex: PlaySound("C:\kenny g.WAV", NULL, SND_ASYNC); 

回答by Ashalynd

Speaking about the path, your data file should be where your executable is, not where your source file is, if the path is not absolute.

说到路径,如果路径不是绝对路径,则数据文件应该是可执行文件所在的位置,而不是源文件所在的位置。

And yeah, this very question has been asked9 years ago ;)

是的,这个问题已经在9 年前被问过了;)

回答by Aldee

you can test by PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS);

您可以通过 PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS); 进行测试。

回答by Vigo

Just in case it is unresolved yet! You need to include the two header files mentioned in previous comments, link the project to the required lib and place the sound file in the same folder as your .exe file (in case you are not using the full path)

以防万一它还没有解决!您需要包含前面注释中提到的两个头文件,将项目链接到所需的 lib 并将声音文件放在与 .exe 文件相同的文件夹中(以防您没有使用完整路径)

回答by M.habib

Try this code it works for me. Also For code::Block use winmm in linker settings.

试试这个代码它对我有用。同样对于 code::Block 在链接器设置中使用 winmm。

#include <iostream>  
#include <windows.h>
#include <MMSystem.h>
 int main(){
PlaySound(TEXT("your file path.wav") , NULL , SND_SYNC) ;
    return 0;
}