Sunday, April 19, 2009

Perl: Play sound using Win32::Sound


Share at Facebook

You can easily play sound from your Perl application. Probably this will only work at Windows platform. Before running your code, you have to install the "Win32::Sound" module. You can use either cpan, or can use ppm to install the module. So the command to install will be any of the below.

ppm install Win32::Sound

cpan -i Win32::Sound


Now write a tiny perl script, and paste the below code on that.

use Win32::Sound;
Win32::Sound::Volume('100%');
Win32::Sound::Play("mywav.wav");
Win32::Sound::Stop();


This small piece of perl code will play your mywav.wav file. And before playing the sound, it will raise the volume to 100%. You can adjust the volume from code, as well as change the wav file location.

Note : the Win32::Sound module may already installed on some perl version. I don't know much about this.




No comments: