PlaySound 在 Visual Studio 中工作,但在独立 exe 中不起作用
我正在尝试使用 Visual Studio 在 C++ 中播放 wav 文件.我将文件my.wav"放在我的项目目录中并使用代码
I am trying to play a wav file in C++ using Visual Studio. I put file "my.wav" in my project directory and use the code
PlaySound(TEXT("my.wav"), NULL, SND_FILENAME | SND_SYNC);
我按下播放按钮(或 F5 甚至 Ctrl-F5),它播放的声音很好.
I hit the play button (or F5 or even Ctrl-F5) and it plays the sound fine.
我打开一个命令提示符并转到 Debug/并运行 MyApp.exe,当它运行时它会播放错误提示音.
I open a command prompt and go to Debug/ and run MyApp.exe and when it runs it plays the error chime.
注意:理想情况下,声音将捆绑在 exe 中,这样我就可以分发 exe 并且它可以工作.我尝试将它放在 Resource.rc 中,但是我在所有示例中看到的代码
Note: Ideally the sound would be bundled in the exe so I can just distribute the exe and it would work. I tried putting it in an Resource.rc but the code I see in all the examples
PlaySound( (char*)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC );
甚至不为我编译.抱怨 IDR_WAVE1,即使这是我的资源名称.
doesn't even compile for me. Complains about IDR_WAVE1 even though that is the name of my resource.
我对老式 win32 有点生疏,但它是这样的:
I'm a little rusty on old school win32 but it was something like this:
在您的文件中包含 resource.h 并使用 MAKEINTRESOURCE
include resource.h in your file and use MAKEINTRESOURCE
PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_RESOURCE | SND_SYNC );