Perl Play WAV Example

From FreeSWITCH Wiki
Jump to: navigation, search

About this script

The following script is a sub routine I wrote to play a wave file in the current session.

Script requirements

* mod_perl
* A WAV file.
* If you're going to use the Dumper line below, please make sure you have 'use Data::Dumper;' - available on most builds and on CPAN.


Example script

Usage: playfile('/usr/local/freeswitch/sounds/music/8000/danza-espanola-op-37-h-142-xii-arabesca.wav');


# This sub routine that plays a WAV file in a current session.  It will give an example how you may set up playback params,
# such as the playback terminator.  In the future, I will add more parameter passing options such as the termination, etc.
#
# by Kareem Hamdy 2009-01-19

sub playfile {


   # Variable initialization - here we assign the
   # parameters passed to their corresponding variables.
 
   my ($string) = @_;
 

   # This line isn't important - however I generally like
   # to see all my variables for troubleshooting.
 
   print "\n\n".Dumper(\@_)."\n\n";
 
 
   # This is where we set the playback application options such as
   # a termination key.  In this example, when the '#' side is pressed,
   # the sound file will stop, and the script will continue on its merry way.
   
   $session->execute("set", "playback_terminators=#");
   
   
   # This line waits until the session is ready.  If so,
   # it will engage the WAV file.    
   
   if ($session->ready ()) {
   
     # This is where the WAV file is executed and injected into the call session.
     $session->execute("playback",$string);
   }
 
   return 1;
 
  
}