Sound stops game?

Jan 2, 2012 at 6:33pm
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);

I tried using ASYNC but no sound is played.
Jan 2, 2012 at 6:59pm
Did you try using SND_FILENAME | SND_ASYNC ?
Jan 2, 2012 at 7:34pm
Yeah I tried that too, no sound plays.
Jan 2, 2012 at 9:31pm
Can you get your sound to play with a little test app which just does something like

1
2
3
4
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);
Last edited on Jan 2, 2012 at 9:31pm
Jan 2, 2012 at 10:24pm
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.
Jan 2, 2012 at 10:36pm
OK. But that was just an experiment, to make sure there was no underlying problem with the async sound.

So now I'm curious about what your doing after you start the non-playing async sound.

(But as I'm in Europe, I won't be back to check the answer till tomorrow now...)
Last edited on Jan 2, 2012 at 10:37pm
Jan 3, 2012 at 5:02pm
Not sure what you mean, after its meant to play the game just continues as usual.
Jan 3, 2012 at 6:07pm
Try this.

1
2
3
4
5
6
7
8
9
 
#include <windows.h>
#pragma comment(lib, "winmm.lib")

int main()
{
  PlaySound("C:\\beep.WAV",    NULL, SND_ASYNC);
  return 0;
}
Jan 3, 2012 at 7:12pm
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;
}


And lo launch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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!
Last edited on Jan 3, 2012 at 7:14pm
Topic archived. No new replies allowed.