问题
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