Dec 27, 2013

HowTo: Play sounds in ActivePerl using Win32::Sounds

Playing of sounds using Win32::Sound requires that you play the sound while the perl script is still running.
If the perl script terminates, the playing of sound also terminates.
The sound plays in this example because we are inside the Gtk2 main loop, the script does not terminate instantly because we are waiting for user inputs.
Please take note of the difference between the 2 buttons, SND_ASYNC allows the Gtk2 to loop without waiting for the sound to finish playing.

On the other hand, the one without the SND_ASYNC does not allow the Gtk2 loop to proceed until the sound has finished playing.

#!/usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Win32::Sound;
my $window = Gtk2::Window->new();
$window->signal_connect('destroy'=>sub{ Gtk2->main_quit();});
my $vbox = Gtk2::VBox->new();
my $b1 = Gtk2::Button->new("NON BLOCKING");
# Win32::Sound::Play('c:\\foobaring.wav',SND_ASYNC);
# you can also use the code above to play your own wav files

my $b2 = Gtk2::Button->new("BLOCKING");
# Win32::Sound::Play('c:\\foobaring.wav');
# you can also use the code above to play your own wav files

No comments:

Post a Comment