Recent

Author Topic: Embedded Sound - play asynchronously?  (Read 2006 times)

rwebb616

  • Full Member
  • ***
  • Posts: 133
Embedded Sound - play asynchronously?
« on: April 23, 2021, 02:43:27 pm »
Hello,

I have the following to play an embedded resource and I'm on the Windows platform:
Code: Pascal  [Select][+][-]
  1. procedure PlayGetReady;
  2. var
  3.   Stream: TResourceStream;
  4. begin
  5.   Stream := TResourceStream.Create(HInstance, 'GETREADY', RT_RCDATA);
  6.   try
  7.     Stream.Position := 0;
  8.     SndPlaySound(Stream.Memory, SND_MEMORY);
  9.   finally
  10.     Stream.Free;
  11.   end;
  12. end;

And I have a getready.wav file added to my project resources.  This all works, however when I go to use the code:

Code: Pascal  [Select][+][-]
  1.   setlabel('GetReady','GET READY!',110,6);  // Display "GET READY...."
  2.   PlayGetReady; // play sound file  

SetLabel displays "GET READY" on the screen and I need the sound file to play at the same time that is being displayed... however even in this order, the sound file plays before "GET READY" displays for some reason. 

I'm trying to embed all needed resources and have the sounds loaded in memory so that they play quickly when needed.  I tested with this:
Code: Pascal  [Select][+][-]
  1. PlaySound('C:\path\to\wav\file\in\some\folder\getready.wav',0,SND_ASYNC);

And using delays I can get it to play just at the proper time but it seems like there would be an easier way and I really don't want to have to include the wav files separately and worry about paths etc.

Again I'm on Windows and no need to do x-platform.

Thanks
Rich

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Embedded Sound - play asynchronously?
« Reply #1 on: April 23, 2021, 03:06:49 pm »
Try:
Code: Pascal  [Select][+][-]
  1.   setlabel('GetReady','GET READY!',110,6);  // Display "GET READY...."
  2.   Application.ProcessMessages;
  3.   PlayGetReady; // play sound file  
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Embedded Sound - play asynchronously?
« Reply #2 on: April 23, 2021, 08:05:30 pm »
Try:
Code: Pascal  [Select][+][-]
  1.   setlabel('GetReady','GET READY!',110,6);  // Display "GET READY...."
  2.   Application.ProcessMessages;
  3.   PlayGetReady; // play sound file  

Yep that was the ticket!  I knew there must be something like update display or something to cause it to display before playing the sound.  Thank You!

 

TinyPortal © 2005-2018