Play audio on Asterisk server with System command

Hello, I would like to run an audio message, using a speech synthesiser on the machine running Asterisk, before an incoming call is heard (rings).

I’m using the following:

exten=>7001,1,GotoIf([ "{DEVICE_STATE(SIP/line1)}" = “NOT_INUSE” ]?dial7001:7002,1)
same=>n(dial7001),System(/home/me/scripts/simple_google_tts en “Call on line 1”)

exten=>7002,1,GotoIf([ "{DEVICE_STATE(SIP/line2)}" = “NOT_INUSE” ]?dial7002:7003,1)
same=>n(dial7002),System(/home/me/scripts/simple_google_tts pt_BR “Call on line 2”)
same => n,Dial(SIP/line2,20,tT)


The System command appears to run, ie. there is a pause for the time it takes to say “Call on line 1”, however there is no sound. I’ve tested it with other non-audio scripts and it works.

If in the Asterisk CLI I enter the following, there is sound:

!sudo /home/me/scripts/simple_google_tts en “Call on line 1”

The script and a corresponding script it uses, have privileges set at 755. I’ve also tried setting ownership to asterisk.asterisk.

The Asterisk CLI shows the following when extension is 7001 called and when it is free:

– Executing [7001@outgoing:1] GotoIf(“SIP/sip-myphone-00000000”, “1?dial7001:7002,1”) in new stack
– Goto (outgoing,7001,2)
– Executing [7001@outgoing:2] System(“SIP/sip-myphone-00000000”, "/home/me/scripts/simple_google_tts en “Call on line 1"”) in new stack
– Executing [7001@outgoing:3] Dial(“SIP/sip-myphone-00000000”, “SIP/line1,20,tT”) in new stack
== Using SIP RTP CoS mark 5
– Called SIP/line1

Can anyone please offer any suggestions?


So this is to announce an incoming call to the room where your Asterisk host’s speaker sits?

My guess is permissions. When you execute the script from the CLI using ‘!sudo’ it works, implying that ‘root’ can do it but the user executing Asterisk doesn’t have access to the audio subsystem.

Maybe adding the user executing Asterisk to the group that owns the audio subsystem would help.

Also, once you get past this problem, you may want to:

  • Append the caller ID to the announcement.
  • Try IBM’s ‘Watson.’ I use it in my application and prefer it to Google.
  • ‘Cache’ the rendered audio so you can ‘instantly’ play the file instead of having to wait for it to be rendered again. I use the ‘md5’ of the text as the file name and check for it’s existence before sending the text out to be rendered.

Thanks sedwards. I had actually added asterisk to the audio group, however I forgot complete to log out and back in again… After doing that the script plays audio, however it seems to bypass PulseAudio and plays directly to the internal sound device of the computer rather an external USB sound card which has beed configured to run with Jack and a PulseAudio Jack Sink. ie. The audio from all PulseAudio apps get channelled through Jack to the external card. It’s important that I use Jack because I need to run other software concurrently that only works with Jack and all software needs to play through the same USB device.

When the following is run as the Asterisk user, sound is sent to the internal device:

sudo -H -u asterisk bash -c ‘unset DISPLAY ; aplay -c 1 -D pulse /home/me/sounds/line1.wav’

The unset DISPLAY is used to avoid an X error. But if i play the following as the user “me”, the audio correctly runs through PulseAudio Jack Sink and through to the USB sound card:

aplay -c 1 -D pulse /home/me/sounds/line1.wav

As well as adding asterisk to the audio group, I also added it to the pulse and pulse-access groups. I notice that a PulseAudio config file gets created in /var/lib/asterisk/.config when aplay is run for the 1st time. Perhaps asterisk is still not accessing it correctly?

Any suggestions welcome!

All the best,

I think the problem was getting sound from the asterisk user to transfer to Jack run by my local user “me”. I found a solution which was to stream audio to the internet on the local host. I used cat and netcat in the Asterisk system call to do this and the audio is received and played, on the correct soundcard via the PulsAudio Jack Sink, by a little daemon using aplay and listening on the same port.

Here is the an example line from extensions.conf which sends audio to and below it is the the script:

same=>n(dial7001),System(cat /home/me/sounds/line1.wav | nc 9876)


trap “exit” INT
while :
nc -l 9876 | aplay -
sleep 0.2
exit 0

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.