[asterisk-users] using Playback() to play a random sound file

Steve Edwards asterisk.org at sedwards.com
Thu May 3 09:58:50 MST 2007


>> Steve Edwards wrote:
>> On Tue, 1 May 2007, Jay Austad wrote:
>>
>>> I've got a directory under /var/lib/asterisk/sounds which contains a bunch
>>> of sound files.  I would like to call the Playback command to play the
>>> files, but I need it to select a file to play randomly.  Is there any way
>>> to do this?
>>
>> I do this with an AGI.

On Wed, 2 May 2007, dave cantera wrote:

> here is a way that I solved a similar problem...  have a shell script that
> runs and indexes all the files in the directory into an ascii flat file with
> a format of
> #### filename
> 0001 directory/tt-weasels
> 0002 directory/tt-monkeys
>
> in your dialplan use the rand() to pick a number, pass it to the shell script
> as an arg[], then the shells script grep()'s and cut()'s the filename puts it
> in a db varaible, the dialplan picks it up and plays it...  as you can see, I
> haven't done it yet :) but, in theory it works...  you could skip the
> dialplan rand() and just use linux rand based on the minutes or seconds value
> for current time...
>
> you don't have to zero fill the index either, I seem to like nicely formated
> files, they are easier for humans to read.
> daveC

Sounds like a lot of effort to avoid writing an AGI. If you have the 
skills to write the script described above, you have the skills to write 
an AGI -- you can write AGI's in shell scripts, btw.

AGI's accept stuff from Asterisk on stdin and send stuff back to Asterisk 
on stdout -- very simple and elegant actually. Take your script and 
rewrite the "reading arguments" bits to read from stdin and change the 
"write db" bits to write to stdout (set a channel variable) and you have 
an AGI and a much cleaner dialplan.

I write AGI's in C for speed and flexibility. No interpreter (bash, perl, 
php, etc.) to fire up, full access to anything you want to do.

In C, I call ftw() ("ftw - traverse (walk) a file tree"). If I get more 
than 1 file, I choose one randomly.

Thanks in advance,
------------------------------------------------------------------------
Steve Edwards      sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                             Fax: +1-760-731-3000


More information about the asterisk-users mailing list