How to stop the PlaySound file while it is playing

I am trying to make a Soundscape in c++ using Visual Studio 2010.

The program allows playing sound. To stop the music, it uses Keybaord and Mouse as detection devices.

The problem that I am having, when the program detected the keyboard/mouse, it doesn't stop immediately. It requires to play the music until the end then be able to stop.

I hope the program can stop the sound file immediately when program detected the keystrokes are pressed or the mouse is moved.

Hope everybody can give me a hand to solve this problem.

Many Thanks!


here is the code that you may want to have look and help for my improvement.

#include "stdafx.h"
#include "windows.h"
#include "scrnsave.h"
#include "resource.h"
#include "mmsystem.h"

LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_PAINT:
PlaySound(TEXT("junko.wav"), NULL, SND_FILENAME | SND_SYNC /* | SND_LOOP */ );
//PlaySound(0,0,0);
break;

default:
return DefScreenSaverProc(hWnd, message, wParam, lParam);
}

return 0;
}


BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
return FALSE;
}

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return TRUE;
}
Last edited on
The WM_PAINT message is probably the worst place to put PlaySound(). Have you monitored how many times this message is received? Check it out and you'll see what I mean. This is probably creating the problem too.

The correct way would be to start the sound asynchronously when the screensaver starts and then cancel it also using PlaySound() (see the function's documentation for details) when input is detected.
I see the point.

I tried to place the PlaySound at different area but no good news.

Is it I need to create a new case for playSound?
You could start playing sound during WM_CREATE or a later event (message). Check out MSDN and see which one serves best for you.
Topic archived. No new replies allowed.