PlaySound not looping when commanded.

Aug 3, 2010 at 6:10pm
Using this code in VC++ 2008 express
1
2
3
4
5
6
7
#include <windows.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;
{
int main ( ) 
PlaySound(TEXT("c:\\ding.wav"), NULL, SND_LOOP);
}


I am unable to get the sound to loop.I've referenced MSDN on how it should work, but I can't seem to get this working. There is obviously something I'm over looking.

Also, I have tried SND_LOOP|SND_ASYNC and a few other combinations hoping it would have an effect.

I was wondering if anyone had a solution. (I removed most of the code because it doesn't seem to have an influence on the function when I commented it out. If you need to see the whole thing I'll post it.)
Aug 7, 2010 at 5:47pm
I am really stumped on this, I haven't made any progress.
Aug 8, 2010 at 7:40am
Doesn't work for me either
Aug 8, 2010 at 8:23pm
Does this work?
PlaySound(TEXT("c:\\ding.wav"),NULL,SND_LOOP|SND_ASYNC);
Aug 9, 2010 at 1:54am
I tried that also but no luck.
Aug 10, 2010 at 12:28am
Try adding cin.get(); This worked for me:

1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#pragma comment(lib,"Winmm.lib")
#include <iostream>
using namespace std;

int main ( ) 
{
PlaySound(TEXT("c:\\ding.wav"),NULL,SND_LOOP|SND_ASYNC);
cin.get();
}
Aug 10, 2010 at 10:41pm
You sir, are the man. It works great now, thank you. Do you mind explaining how console input fixed it.
Last edited on Aug 10, 2010 at 10:44pm
Aug 10, 2010 at 10:59pm
It probably works because if you don't have a "pause" there, then the program ends before the sound even starts playing, since you aren't blocking and waiting for it to finish.
Aug 11, 2010 at 12:06am
I see, and the reason you dont use system(PAUSE) is because an input is more simple and uses less resources?
Aug 11, 2010 at 1:13am
Yes, and system() also opens a security hole in your program, so you will want to avoid using it if at all possible.
Aug 11, 2010 at 2:01am
Ahhhh, awesome. Thank you for the tip.
Aug 11, 2010 at 3:18am
Yeah I'm pretty sure it just needed a sort of pause so the program wouldn't end suddenly. It was a lucky guess lol.
Topic archived. No new replies allowed.