Playing a sound and dealing with a keystroke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thenath24
    New Member
    • Apr 2007
    • 10

    Playing a sound and dealing with a keystroke

    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

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

    Comment

    • thenath24
      New Member
      • Apr 2007
      • 10

      #3
      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?;


      Code:
      //code that I want to generate a sound
      case VK_SPACE:
      					unsigned long iID;
      					hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PlaySound,NULL,0,&iID);
      					CloseHandle(hThread);
      				break;
      Code:
      //function I want to be called when the new thread is created
      void PlaySound(long lParam)
      {
      	PlaySound("GUN01.WAV",NULL,SND_NODEFAULT);
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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:
        [code=c]
        DWORD WINAPI ThreadProc(LPVO ID);
        [/code]

        So, you have to write a funtion like:
        [code=c]
        DWORD WINAPI theNath24(LPVOI D arg)
        {
        //use arg to point to a struct whose member contain
        //the arguments for PlaySound()
        //Fish out the arguments.
        //You call PlaySound() here
        }
        [/code]

        Then you make your CreateThread call:
        [code=c]
        struct Parms
        {
        //Put your arguments in here as members
        };
        Parms* args = new Parms;
        //Load args members with the values for PlaySound()
        //
        hThread = CreateThread(NU LL,0, theNath,args,0, &iID);
        //
        //off you go.
        [/code]

        Comment

        Working...