Connecting Tech Pros Worldwide Forums | Help | Site Map

Playing a sound and dealing with a keystroke

Newbie
 
Join Date: Apr 2007
Posts: 10
#1: Oct 26 '07
Hello all,

I'm currently writing a 3D Game demo and i'm having trouble getting the program to play a sound and deal with a keystroke at the same time.

If a sound is playing the program will wait for the sound to finish before dealing with a user input, this is obviously no good if I want music playing through out i've tried finding examples of how to do this but have had no luck, can anyone help???

Thanks in advance

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#2: Oct 27 '07

re: Playing a sound and dealing with a keystroke


Use multithreading. Your sound should be on a worker thread allowing the mainline to conunue while the sound is playing.
Newbie
 
Join Date: Apr 2007
Posts: 10
#3: Oct 30 '07

re: Playing a sound and dealing with a keystroke


Quote:

Originally Posted by weaknessforcats

Use multithreading. Your sound should be on a worker thread allowing the mainline to conunue while the sound is playing.

Hi there,

Thanks for the tip, having read into multithreading on the MSDN library I tried to implement a simple example given in my program.

While it didn't give me any errors, it also didn't work, have I missed something?;


Expand|Select|Wrap|Line Numbers
  1. //code that I want to generate a sound
  2. case VK_SPACE:
  3.                     unsigned long iID;
  4.                     hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PlaySound,NULL,0,&iID);
  5.                     CloseHandle(hThread);
  6.                 break;
  7.  
Expand|Select|Wrap|Line Numbers
  1. //function I want to be called when the new thread is created
  2. void PlaySound(long lParam)
  3. {
  4.     PlaySound("GUN01.WAV",NULL,SND_NODEFAULT);
  5. }
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#4: Oct 30 '07

re: Playing a sound and dealing with a keystroke


The function argument in CreateThread for the address of your PlaySound function has to be a ThreadProc (you might look that up).

A ThreadProc can be any function that has this form:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI ThreadProc(LPVOID);
  2.  
So, you have to write a funtion like:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI theNath24(LPVOID arg)
  2. {
  3.     //use arg to point to a struct whose member contain
  4.     //the arguments for PlaySound()
  5.     //Fish out the arguments.
  6.     //You call PlaySound() here
  7. }
  8.  
Then you make your CreateThread call:
Expand|Select|Wrap|Line Numbers
  1. struct Parms
  2. {
  3. //Put your arguments in here as members
  4. };
  5. Parms* args = new Parms;
  6. //Load args members with the values for PlaySound()
  7. //
  8. hThread = CreateThread(NULL,0, theNath,args,0,&iID);
  9. //
  10. //off you go.
  11.  
Reply