PlaySound(); Problem

Dec 31, 2012 at 4:44am
Hello all. I've been trying to use the PlaySound(); function with no success. Basically what is happening is i'll build and run the program, the console will appear giving me a sliver of hope... then all of a sudden I get an error "System could not find the path specified." I've quadruple checked the path and it seems right, I moved it around different places as well. The last place I put it was in the project folder. I'm not sure exactly whats up with it. I added winmm.lib in my linker settings, and here's what my code looks like.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <windows.h>
#include <mmsystem.h>

using namespace std;

int main()
{
PlaySound("g.wav",NULL,SND_FILENAME);
return 0;
}



Any advice?? I'm using CodeBlocks as my IDE and MinGW as my compiler... Thanks in advance!!
Dec 31, 2012 at 5:18am
Use GetCurrentDirectory to debug what is the working directory. If is not the directory with your .wav file then problem is solved.
Dec 31, 2012 at 5:19am
or try putting the whole path::: including c:\\ or whatever is your drive letter
Dec 31, 2012 at 5:31am
The indicated approach is to use GetModuleFileName and PathRemoveFileSpec to always get the EXE directory, no need to hardcode full path, just put .wav file in the same folder as the exe then.
Dec 31, 2012 at 5:57pm
Nah, you should use GetCurrentDirectory (first modoran's post on the topic) and strcat the filename, because if you just use 'g.wav' and this file is in some other path, but still in the PATH environment, that specific 'g.wav' will be played.

Test?
Not sure, but try putting some 'Never Gonna Give You Up' wave file as 'g.wav' in the same directory of explorer.exe and run again your program.
Last edited on Dec 31, 2012 at 5:57pm
Dec 31, 2012 at 6:33pm
Nah, you should use GetCurrentDirectory and strcat the filename


That will be useless work, as the working directory is searched first anyway if you just use a relative path.

Please consider that is a difference between working directory and executable directory.
Dec 31, 2012 at 6:42pm
modoran wrote:
Please consider that is a difference between working directory and executable directory.

You can explicitly specify the working directory, I know. You should use the working directory, not the executable directory anyways, because of that reason.
modoran wrote:
That will be useless work, as the working directory is searched first anyway if you just use a relative path.

But what if you wanted, in case g.wav didn't exist, to run h.wav? If g.wav is in another path, you didn't hear the h.wav but heard a wrong g.wav.
Anyways, I think it's slower to use a relative file name than an absolute one.
Topic archived. No new replies allowed.