Friday, February 13, 2015

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();
$window->add($vbox);
my $b1 = Gtk2::Button->new("NON BLOCKING");
$b1->signal_connect(clicked=>sub{
Win32::Sound::Play(SystemExclamation,SND_ASYNC);
# Win32::Sound::Play(c:\foobaring.wav,SND_ASYNC);
# you can also use the code above to play your own wav files
});
$vbox->pack_start($b1,0,0,0);

my $b2 = Gtk2::Button->new("BLOCKING");
$b2->signal_connect(clicked=>sub{
Win32::Sound::Play(SystemExclamation);
# Win32::Sound::Play(c:\foobaring.wav);
# you can also use the code above to play your own wav files
});
$vbox->pack_start($b2,0,0,0);
$window->show_all();
Gtk2->main();

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.