I'm learning how to put music into my programs and the music plays perfectly while running through CodeBlocks, but as soon as I run my program through .exe that I copied to my desktop, the computer alarm sounds rather than the music, as if I was using a bunch of \a's in my program. How can I fix this?
SND_FILENAME indicates that you are passing the name of a sound file to the PlaySound() function. Most likely the path is no longer correct if the .exe is copied to a different location. Try using an absolute path including drive and folder name, for the sound files.
from MSDN documentation:
SND_FILENAME The pszSound parameter is a file name. If the file cannot be found, the function plays the default sound unless the SND_NODEFAULT flag is set.
Edit: It seems you have copied the .exe file to the desktop. You might instead create a shortcut on the desktop, but leave the exe in its original location, which might be a quick fix to the problem.
To clarify: I have this project saved under Windows(C:) > C++ Programs > sound test 2, with wave files "sound" & "sound2" inside the project named "sound test 2".
When I open this project inside CodeBlocks and run it, it runs and plays perfectly.
Now when I go here - Windows(C:) > C++ Programs > sound test 2 > bin > debug - there is an .exe file called "sound test 2" which I copy and pasted unto my desktop (so that I can play it without needing to open CodeBlocks).
I close CodeBlocks and double-click the "sound test 2" icon and it runs good except it plays the alerts sound. I close that and open CodeBlocks and run my program and it works perfectly again.
When I copy & paste my .exe file why wouldn't it copy my wave files as well? Isn't my wave files inside the .exe file? I don't get it.
"If the file cannot be found, the function plays the default sound".
Why can CodeBlocks find it but not the .exe file? Do I need to change this somehow? -
PlaySound("sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
Thanks
Edit: I get the default sound even if I just double-click on the .exe file in my C++ programs, I don't even need to copy & paste it to my desktop. So it seems that the wave files aren't in my .exe file even though they are in my program name "sound test 2" file. Why?
I tried creating a shortcut to the desktop rather then copy & paste, but the result is the same.
Fixed it. Thanks Chervil for your help. Rather than copy & pasting to the desktop you must choose Send to Desktop (create shortcut).
And then I found another key ingredient I was missing in an old stack overflow post. You NEED to have your wave files in bin > debug (where your .exe file is) as well as where your project is.
Actually, upon further testing you only need your wave files in bin > debug.
Phew, I'm glad this is solved. It's been a rough couple of days.
No, the wave files are not inside the exe. Instead the program reads them from the drive when running the program.
When the program runs, it reads (or tries to) the sound files from the current working directory. Often the working directory is the same as that of the exe file. However, when running from within Code::Blocks, it sets the program to use the same directory as the source files (the .cpp files).
The reason that creating a shortcut to the exe didn't work is because the working directory now becomes
Is one way better than the other, or a matter of preference?
Well, I think it depends, initially I thought it was better to use the long file name including the drive full path. That does mean you could copy the exe anywhere you liked on your machine, and it would still work.
But what if you wanted to share the program with a friend? They would not have the same drive/folder structure on their own machine. So in this case it would make more sense to go back to your original code with the short (relative) filename. Then you could share the program with others more easily. For example you might put the three files (.exe and two sound files) inside a single .zip file for easy transport. Your friend then just unzips the contents into any folder they wish.
There is another way, using a resource file. This way, the sound files can stored inside the exe file, so the end result is a single file, but it is much larger because it contains the wave files as well as the executable program. This can also be convenient. The advantage/disadvantage is that because there is just one file, the only way to swap the sound files is to rebuild the program, rather than just replacing the sound file later.