Hey, I'm using the PlaySound function for my game but when it plays, it stops the game until its finished playing. How do I get it to play without stopping the game?
I'm using PlaySound(TEXT("test.wav"), NULL, SND_FILENAME);
Yeah that works, but it still makes the game stop because of sleep, I tried to reduce the time it sleeps for but the game noticeably stops while playing it and ruins the game.
I was wondering if there was something going on which was disrupting the sound playback. If the code runs ok with the sleep, then something appears to be upsetting the playback when you don't sleep.
Out of interest, you could try playing the sound on another thread.
Declare a thread proc
1 2 3 4 5 6 7 8 9 10
DWORD WINAPI PlaySoundThreadProc (LPVOID threadParam)
{
// could prob play sound sync here instead
PlaySound(TEXT("test.wav"), NULL, SND_FILENAME | SND_ASYNC);
Sleep(5000); // or whatever you can stand!
PlaySound(NULL, NULL, SND_FILENAME | SND_ASYNC);
Sleep(1000);
return 0;
}
HANDLE hThread = CreateThread(
NULL, // default security
0, // default stack size
PlaySoundThreadProc,
NULL, // no param
0, // no flags
NULL ); // don't need the ID
if(NULL != hThread)
{
// close handle immediately. Thread will exit when it's done
// (this does assume the games carries on running long enough for
// the sound to play)
CloseHandle(hThread);
}
else
{
DWORD dwError = GetLastError();
cerr << "Thread failed to start: error = " << dwError << endl;
}
Andy
PS @Michael McCody -- as you exit main immediately after calling PlaySound to start playing the sound, the system prob doesn't even have enough time to load the file before the program exits!