Search code examples
c++visual-studiowinapiplaysound

PlaySound works in Visual Studio but not in standalone exe


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.


Solution

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