How do I make my console output sound?

I am making a game and I would like to add sounds to it. My problem is that my console will not output sound. Specifically, I would like to use PlaySound() to play .wavs, but I can't even get cout << "\a"; to work or the Beep () function to work. My code compiles and runs properly, but no sound comes out. I included windows.h. The .wav file I was using does work when played in a music player, and it was included as a resource file.

My speakers are on and not muted. My computer plays all other sounds properly.
I have looked throughout the Volumes controls, and the Sound section of the Control Panel. I have looked around in all the options I can think of in my compiler (which is Visual Studio 2008, by the way) to find some place that may have sound defaulted to mute. Alas, I can find no such option.

I believe that there is an option somewhere that is muting my compiler, and I cannot think of anywhere else that could affect it. Are there any places anyone can think of that I might be overlooking? Thank you in advance.

P.S. If this belongs in a more advanced forum, please let me know.
> My speakers are on and not muted. My computer plays all other sounds properly.

Then your code is wrong...
closed account (z05DSL3A)
If you are using Vista, there is a volume Chanel for running applications.

Start Visual Studio, click on the speaker icon in the task bar and select mixer, you should then see the various channels there.
Then your code is wrong...


Shouldn't this output sound? At least one of them?
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "\a\a\a\a\a\a\a\a";
	Beep (500,50);
	return 0;
}

I get nothing. Am I not including something necessary into my code?

On the plus side, I've managed to get PlaySound to play a sound...kind of. This does output sound.
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#pragma comment( lib, "winmm" ) 
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS);
	return 0;
}


However, I'm still having trouble getting it to output wavs that I input as a resource file.

I am adding them by going to the Resource Files in the Solution Explorer, Add Resource... > Import... > choose my wav file.
The code will compile, but nothing comes out.
To test it, I'm trying
PlaySound(TEXT("Final Fantasy XIII - Battle Theme"), NULL, SND_ASYNC | SND_LOOP);
and
PlaySound(TEXT("Final Fantasy XIII - Battle Theme"), NULL, SND_RESOURCE | SND_ASYNC);

I believe my problem may be that I need to #include the wav file at the top of my source code, but I'm not getting that to work. Perhaps I'm writing what to include improperly.

Does anyone know how I can properly work a wav file into my project?
Try this, if you're using Visual Studio.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <windows.h>

#pragma comment(lib,"Winmm.lib")

using namespace std;

int main () {
	PlaySound("C:\\stacchetto.wav", 0, 0);
	
	cin.ignore();
	return 0;
}


the .wav file must be put in your C: drive for this code to work (or you can edit the address to suit your needs) and its name must be "stacchetto". Let us know if it works!

Regarding the '\a' escape sequence, it may fail on some hardware. On my hardware, for example, it stopped working after I've changed dvd-reader and case ^^'
I don't know if there's a fix for this though.
Last edited on
Topic archived. No new replies allowed.