PlaySound without "freeze"

Hi,
I want to add sound to my program, but everything "freezes" until the sound is over. It prints '1', then plays sound, and when the sound ends, it prints '2'. I want print '1', play sound, and when sound is playing, print '2'.

My code:

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

using namespace std;

int main()
{
    cout << 1;
    PlaySound("C:/WINDOWS/Media/ding.wav", NULL, SND_FILENAME);
    cout << endl << 2;

    return 0;
}


I tried also this...

 
PlaySound("C:/WINDOWS/Media/ding.wav", NULL, SND_ASYNC);


...and this...

 
PlaySound("C:/WINDOWS/Media/ding.wav", NULL, SND_FILENAME || SND_ASYNC);


...but in last two cases program just shows numbers, without playing sound.
Try something like this:
1
2
3
4
5
6
7
8
9
10
    for(int i = 1; i < 100; i++)
    {
        Sleep(250);  //250ms, so that the numbers don't count up too fast
        if(i == 10)  //on the 10th iteration of the loop start playing the sound
        {
            PlaySound("C:/WINDOWS/Media/notify.wav", NULL, SND_ASYNC);
        }
        //you should still get numbers to print while the sound is playing
        cout << endl << i;
    }
The SND_ASYNC flag works, but your program is so short that the process terminates before the sound has a chance to play.
booradley60 is correct. With the SND_ASYNC flag, the function returns immediately after starting the sound. Your main method returns before you have a chance to actually hear the sound.
It works, many thanks!
Topic archived. No new replies allowed.