palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I used Win32::Sound::Play('SystemExit');
and were very pleased with the result until I noticed my code freezes until the sound ends.
So I tried to make the sound run in the background,
while letting my code keep running without waiting for the sound to end.
The "how to" description talks about "flags".
I'm not sure if there is a problem with the module,
or if I just can't figure out what "flags" means.

These are (some of) the forms I tried to do this:
(individually tried of course)
Win32::Sound::Play('SystemExit',('SND_ASYNC')); Win32::Sound::Play('SystemExit',(SND_ASYNC)); Win32::Sound::Play('SystemExit',['SND_ASYNC']); Win32::Sound::Play('SystemExit',[SND_ASYNC]); Win32::Sound::Play('SystemExit',SND_ASYNC); Win32::Sound::Play('SystemExit',-SND_ASYNC); Win32::Sound::Play('SystemExit',- SND_ASYNC); Win32::Sound::Play('SystemExit',[-SND_ASYNC]); Win32::Sound::Play('SystemExit',[- SND_ASYNC]); -SND_ASYNC Win32::Sound::Play('SystemExit'); - SND_ASYNC Win32::Sound::Play('SystemExit'); -SND_ASYNC(Win32::Sound::Play('SystemExit')); - SND_ASYNC(Win32::Sound::Play('SystemExit')); Win32::Sound::Play('SystemExit',[SND_ASYNC=>1]); Win32::Sound::Play('SystemExit',[1]); Win32::Sound::Play('SystemExit',[$SND_ASYNC]); Win32::Sound::Play('SystemExit',($SND_ASYNC)); Win32::Sound::Play('SystemExit',$SND_ASYNC); SND_ASYNC{Win32::Sound::Play('SystemExit');} SND_ASYNC{Win32::Sound::Play('SystemExit');};
As you can guess, none did the trick.
Just look at all these annoying combinations
(it's even more painful to look at them all stacked up like that)

please help :-[

Update: Resolved thx :-)

Replies are listed 'Best First'.
Re: Win32::Sound module problem
by Anonymous Monk on Jul 18, 2011 at 06:49 UTC

    please help

    You forgot to use Win32::Sound; :D

    It seems, you need the process (thread) to live, for the sound to get played

    This (sleep) works, it makes noise

    perl -MWin32::Sound -e " Win32::Sound::Play( qq[$ENV{WINDIR}/media/cho +rd.wav] , SND_ASYNC ); sleep 3; Win32::Sound::Stop(); "
    Omit the sleep and it doesn't make noise. Looping also works
    perl -MWin32::Sound -e " Win32::Sound::Play( qq[$ENV{WINDIR}/media/cho +rd.wav] , Win32::Sound::SND_ASYNC() | Win32::Sound::SND_LOOP() ); sle +ep 3; Win32::Sound::Stop(); "

    Don't mess with volume, that is users job :)

      Don't worry, I did "use" in my code ^^.

      Thank you very much you're totally right.
      I thought the "play" function makes windows itself play the sound (independently to the program),
      which is of low probability when considering the default waits the sound to end before continuing the program.
      My "play" call really was the last code line (was a test), so my sound probably played for just a microsec XD (nice catch).
      Thx again :-]

      Btw: I'm with you on the volume thing (which from some reason doesn't do the percentages right), but I do like that I have the option ^^.
Re: Win32::Sound module problem
by kejohm (Hermit) on Jul 18, 2011 at 01:57 UTC

    The 5th version should have worked, Win32::Sound::Play('SystemExit',SND_ASYNC);. You may need to set the volume using Win32::Sound::Volume('100%'); before playing the sound. Check the docs for more info.

      Thx but nope.
      No volume problems (it played it as should in the synchronously form).
      But just in case, I tried it anyway with the volume preset, but no change.