Music not playing through .exe

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?

Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <windows.h>
#include <MMSystem.h>

using namespace std;

int main ()
{
    string name;
    int age;
    char input;

    PlaySound("sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);

    cout << "Hello, what's your name? :" << endl;
    getline(cin, name);
    cout << endl << endl;
    cout << "How old are you? :" << endl;
    cin >> age;
    cout << endl << endl;
    cout << "That's a lovely age, " << name << "." << endl << endl << endl;
    cout << "Do you like this song? :";
    cin >> input;
    cout << endl << endl;

    if (input == 'Y' || input == 'y')
    {
        cout << "I'm so very glad!" << endl << endl;
        cout << "Here's one more..." << endl << endl;
    }

    else
    {
        cout << "That makes me sad..." << endl << endl;
        cout << "Maybe you'll like this one better..." << endl << endl;
    }

    PlaySound("sound2", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);

    cout << endl << endl << "Press Enter to exit.";
    cin.sync(), cin.get();

    return 0;
}
.exe that I copied to my desktop

Do you mean you copied the executable to another pc? Or just in another folder?
Because it sounds like you're missing some library, doesn't it?
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.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680%28v=vs.85%29.aspx


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.
Last edited on
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.
Last edited on
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.
Isn't my wave files inside the .exe file?
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
 
C:/C++ Programs/sound test 2/bin/debug/

but the sound files are actually in
 
C:/C++ Programs/sound test 2/


One solution is to change this:
 
PlaySound("sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
to this:
 
PlaySound("C:/C++ Programs/sound test 2/sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);


and similarly for the other PlaySound().

That should mean the program will always look in the right place for the sound files, no matter how it is run.

By the way, that's what I meant when I previously said, "Try using an absolute path including drive and folder name, for the sound files.".
Last edited on
Chervil, I tried it your way, and it works as well!

So you can either place your wave files in bin/debug and keep this -
PlaySound("sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);


Or you can place your wave files where your project is and change it to where your wave file is, in my example -
PlaySound("C:/C++ Programs/sound test 2/sound", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);


Is one way better than the other, or a matter of preference?

Thanks again.
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.

Simple example using a resource file:
http://www.cplusplus.com/forum/general/188070/#msg916002

With Code:Blocks, if you just add the sounds.rc file to the project along with main.cpp, then the build process should work ok.
Last edited on
Thank you again for your help, and your insight.
Topic archived. No new replies allowed.