winapimingwwin32guiplaysound

WinAPI: How to make sound effect keep up with each mouse clicking


Ensure that sound was played after each clicking the mouse.
My idea is to create a thread that runs constantly.

#include <pthread.h>
music ding1("./ding1.wav");
music ding2("./ding2.wav");
void* pmusic(void* a)
{
  while(1)
  {
    DWORD dw=WaitForSingleObject(hmusic, INFINITE) ;
    if(ding1.busy)
    {
      ding2.play();
    }else{
      ding1.play();
    }
    ResetEvent(hmusic);
  }
}

Create a public signal.

HANDLE hmusic=CreateEvent(nullptr,false,false,nullptr);

Play the sound using the playsound function,in music class

class music
{
public:
music(char* path)
{
  //load the wav file to memory
  fs.open(path...);
  ...
  fs.readsome(buf...);
  ...
}
play()
{  
   busy=1;
 
 PlaySoundA(buf,null,SND_MEMORY,SND_ASYNC,SND_NOSTOP,SND_NOWAIT);
  busy=0;
}

char * buf;
int busy;
...
}

WndProc

LRESULT CALLBACK WndProc(hwnd,msg,wparam,lparam)
{
  switch(msg)
  case WM_LBUTTONDOWN:
  {
    SetEvent(hmusic);
    break;
  }
  case WM_LBUTTONUP:
  {
    ResetEvent(hmusic);
    break;
  }
  case WM_CREATE:
  {
    pthread_create(&tid,null,pmusic,null);
    break;
  }
}

It worked on Windows 10 after compiling, BY mingw32 with no problem.
Maybe there is another different way to achieve the above.
Thank you for sharing your wisdom and experience.


Solution

  • I suggest to read the doc for PlaySound.

    According to that, SND_NOWAIT is not supported. The SND_NOSTOP will prevent you from playing a sound if another one is already playing, that goes against your plan. Also, the flags are combined with the bit-wise OR operator |, not comma:

    PlaySoundA(buf, NULL, SND_MEMORY | SND_ASYNC);
    

    As was pointed in the comments, you don't need a thread to play with SND_ASYNC.

    Don't type the code into the question; copy/paste the working code from your editor; the one you have won't compile.

    Here is what you may use:

    LRESULT CALLBACK WndProc(hwnd,msg,wparam,lparam)
    {
      switch(msg) {
      case WM_LBUTTONDOWN:
        music();
        break;
      }
    ....
    

    Where music() is:

    void music()
    {
      if(ding1.busy)
        ding2.play();
      else
        ding1.play();
    }