PlaySound works in Visual Studio but not in standalone exe

▼魔方 西西 提交于 2019-12-13 08:19:13

问题


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);

I hit the play button (or F5 or even Ctrl-F5) and it plays the sound fine.

I open a command prompt and go to Debug/ and run MyApp.exe and when it runs it plays the error chime.

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 );

doesn't even compile for me. Complains about IDR_WAVE1 even though that is the name of my resource.


回答1:


I'm a little rusty on old school win32 but it was something like this:

include resource.h in your file and use MAKEINTRESOURCE

PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_RESOURCE | SND_SYNC );



回答2:


As I recall it you need to 'link' the resource file with a resource script file ".rc file" in Visual Studio to embed it inside the .exe file. Otherwise you need to load it like @wilx points out.



来源:https://stackoverflow.com/questions/36424691/playsound-works-in-visual-studio-but-not-in-standalone-exe

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