Mabye someone over here can explain to me what am I doing wrong. This is after reading a lot of articles over the net and doing what the articles say should work but it is not working for me.
I am developing a nice little game with a background music and an explosion sound. For the explosion I know I need to use threads or my music stops when the first explosion happens. I am using threads but the music still stops.
I need the background music to keep on playing all the time and it should continue playing during and after the explosion sound. I tried playing the explosion sync, It doesn’t make any difference, the background sound stops playing the second the thread method is triggered.
Here is my code. It is very simple, but the explosion sound is not working.
-
This is the GLOBAL decleration in the *.h file:
UINT CMonstersThread(LPVOID Param); -
This is the thread function in the *.cpp file:
UINT CMonstersThread(LPVOID Param)
{
PlaySoundA("sounds\\expl06.wav", NULL, SND_ASYNC);
AfxEndThread(0);
return FALSE;
}
-
This is the call for the thread every time a “friendly” is hit, (in the same *.cpp file):
AfxBeginThread(CMonstersThread,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL);
That is all my code. And from what I got over the web, it should work but isn’t playing the music continuously while making explosion sounds as I expect it should.
I would suggest using XAudio2 from the latest DirectX sdk to play your audio. I will take a little more work & code, but the end result will be better because you will be able to load the sound file separately from playing it.
With ‘PlaySound’ you will notice a lag in the audio if you try to use it right after an event, like a mouse click or a monster dying / explosion and you won’t hit this with XAudio2. I know this from experience.
Since you are already using Visual Studio, I also suggest you try using VS 2010 if possible because the [Concurrency Runtime, Parallel Pattern Library and Agents Library]]1 make threading and tasking take a little less code.
There are samples as well at http://code.msdn.com/concrtextras and you may find something useful here as well.
-Rick