Recent

Author Topic: rejecting button click events while playing a sound file.  (Read 821 times)

lazer

  • Sr. Member
  • ****
  • Posts: 269
rejecting button click events while playing a sound file.
« on: September 23, 2025, 11:37:14 am »
Hi,

I need to play a sound (snd_sync) when a button is clicked . Simple enough.

But I need to prevent multiple click events piling up while the sound is playing.

How can I clear the buffer after the sound play procedure returns or process mouse click events with EXIT while the sound is playing?

TIA.

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: rejecting button click events while playing a sound file.
« Reply #1 on: September 23, 2025, 12:32:40 pm »
Disconnect the event handler.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

wp

  • Hero Member
  • *****
  • Posts: 13329
Re: rejecting button click events while playing a sound file.
« Reply #2 on: September 23, 2025, 12:44:31 pm »
In the OnClick handler of the button, set Enabled of the button and other controls which should not be clickable to false - this way the users have a visual feedback that they cannot click any more. Later, when the sound is finished, set Enabled back to true again.

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: rejecting button click events while playing a sound file.
« Reply #3 on: September 23, 2025, 01:43:28 pm »
Quote
set Enabled of the button and other controls which should not be clickable to false
Ah yes. That's a good simple solution.
... but it doesn't work.

It seems the mouse events are being buffered somewhere and are still getting processed when I re-enable later.

I need a way to signal these events as having been treated.




Disconnect the event handler.

Thanks. I could only find how to remove additional handlers. How do I remove the default one?
« Last Edit: September 23, 2025, 02:10:42 pm by lazer »

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: rejecting button click events while playing a sound file.
« Reply #4 on: September 23, 2025, 02:24:23 pm »
Hi
Somewhat like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnPlayClick(Sender: TObject);
  2. begin
  3.   btnPlay.OnClick:= nil;
  4.   sndPlay('pingeling.wav');
  5.   btnPlay.OnClick:= @btnPlayClick;
  6. end;
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: rejecting button click events while playing a sound file.
« Reply #5 on: September 23, 2025, 04:56:08 pm »
Thanks, makes sense but this is not working as it should.

Code: Pascal  [Select][+][-]
  1. procedure TspellWin.PlayWAVfile(soundfilename:ansistring; flags:integer);
  2. begin
  3.   SpeakBtn.onclick:=nil;
  4.   soundplayer.SoundFile:=soundfilename;
  5.   soundplayer.PlayStyle:=psSync; // use flags ###
  6.   soundplayer.Execute;
  7.   Application.messagebox(Pchar(soundfilename ),'play sound returned.',mb_OK);
  8.   SpeakBtn.onclick:=@menuCardSpeakClick;
  9. end;
  10.  

The sound sample is about one second long.

With the messagebox() call it looks good.
The msg pops up AFTER the sound has played.  If I rapidly click twice I only get one sound then the msg.
That is the required functionality.

Without the  messagebox() call it misbehaves.
if I rapidly click twice,  I get two sound plays.

This does not seem consistent to me, so I must be missing something.
« Last Edit: September 23, 2025, 05:01:20 pm by lazer »

dseligo

  • Hero Member
  • *****
  • Posts: 1650
Re: rejecting button click events while playing a sound file.
« Reply #6 on: September 23, 2025, 05:20:40 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TspellWin.PlayWAVfile(soundfilename:ansistring; flags:integer);
  2. begin
  3.   If SpeakBtn.Enabled then
  4.   begin
  5.     SpeakBtn.Enabled := False;
  6.     Application.ProcessMessages;
  7.     soundplayer.SoundFile:=soundfilename;
  8.     soundplayer.PlayStyle:=psSync; // use flags ###
  9.     soundplayer.Execute;
  10.     // Application.messagebox(Pchar(soundfilename ),'play sound returned.',mb_OK);
  11.     SpeakBtn.Enabled := True;
  12.   end;
  13. end;

P.S.: Added ProcessMessages, so hopefully button shows disabled.

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: rejecting button click events while playing a sound file.
« Reply #7 on: September 23, 2025, 05:56:26 pm »
Nope , that's the same.

I've also noted, now it goes grey state that even though the button is disabled, I'm still getting the second audio sound .
« Last Edit: September 23, 2025, 06:49:01 pm by lazer »

dseligo

  • Hero Member
  • *****
  • Posts: 1650
Re: rejecting button click events while playing a sound file.
« Reply #8 on: September 23, 2025, 08:34:56 pm »
I've also noted, now it goes grey state that even though the button is disabled, I'm still getting the second audio sound .

You are correct, there is something strange here.

I created test project (in attachment) with Sleep instead of playing sound, so it doesn't depend on sound files to run. It's the same effect.

When you click on a button while it's disabled, it is 'remembered' and clicked after it's enabled again. Can someone comment why is this the case?

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: rejecting button click events while playing a sound file.
« Reply #9 on: September 23, 2025, 08:58:47 pm »
Hi
Quote
Can someone comment why is this the case?
The mouse clicks are queued in the messaging system, thus when you reenable the button again, it resumes its duty of pumping the message-queue.
I think, that to avoid this situation, one has to play the sound in a "fire-and-forget" thread and then let the onclick-handler ignore clicks until the thread finishes and sets or resets a flag, i.e.: when the thread is running, the event-handler just lets the clicks pass through untouched, meaning -- you can click till the cows come home - nothing happens until the thread finishes and resets the flag...
Just my 2 cent's worth
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: rejecting button click events while playing a sound file.
« Reply #10 on: September 23, 2025, 09:10:29 pm »
Quote
The mouse clicks are queued in the messaging system, thus when you reenable the button again, it resumes its duty of pumping the message-queue.
Yes, that was my expectation from working in TPW decades ago which was a much more like direct API programming.

Quote
one has to play the sound in a "fire-and-forget" thread and then let the onclick-handler ignore clicks until the thread finishes

Well I was expecting playing async would do that but that way I never get a callback to know when that thread has ended. I cannot  "fire-and-forget" .



lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: rejecting button click events while playing a sound file.
« Reply #11 on: September 23, 2025, 10:14:17 pm »
Ah right, I've got it.

Code: Pascal  [Select][+][-]
  1. procedure TspellWin.PlayWAVfile(soundfilename:ansistring; flags:integer);
  2. begin
  3.   SpeakBtn.onclick:=nil;
  4.   soundplayer.SoundFile:=soundfilename;
  5.   soundplayer.PlayStyle:=psSync; // use flags ###
  6.   soundplayer.Execute;
  7.   Application.ProcessMessages;    //speaking:=false;
  8.   //Application.messagebox(Pchar(soundfilename ),'play sound returned.',mb_OK);
  9.   SpeakBtn.onclick:=@menuCardSpeakClick;
  10. end;
  11.  

The ProcessMessages needs to be aftter the call to  soundplayer and with the nul handler.
That flushes the queue of untreated messages.

Thanks to all for the help getting my ideas straightened out.
« Last Edit: September 23, 2025, 10:17:54 pm by lazer »

 

TinyPortal © 2005-2018