Asterisk. Music on hold

In this article, I will provide examples of changing music on hold, force music on hold, and more.

For Music on hold to work, a module must be loaded in the /etc/asterisk/modules.conf file:

load => res_musiconhold.so

You can check if it is loaded:

asterisk -rvv
module show like res_musiconhold.so
exit

The configuration is located in the /etc/asterisk/musiconhold.conf file, for example:

[default]
mode=files
directory=moh

[newclass]
mode=files
directory=/var/lib/asterisk/moh/newclass
random=yes

You can also see it in the asterisk console:

asterisk -rvv
moh show classes
moh show files
exit

moh, by default, this directory is /var/lib/asterisk/moh, but you can also specify the full path, if the files are in the specified directory, then they are automatically played in random order, for example, when a call is put on hold or picked up and then transferred to another number.

Good free music can be found for example in Youtube Music Library (https://www.youtube.com/audiolibrary)

In Linux, I converted audio files to wav 16 bit 8khz mono format, I will give an example of commands:

ffmpeg -i ixnfo.com.mp3 -acodec pcm_s16le -ar 8000 -ac 1 ixnfo.com.wav
sox ixnfo.com.wav -r 8000 -c1 ixnfo.com.gsm lowpass 4000 compand 0.02,0.05 -60,-60,-30,-10,-20,-8,-5,-8,-2,-8 -8 -7 0.05

After converting, be sure to check if asterisk can read the generated sound file.

Here’s a simple example of playing music on hold immediately after picking up the handset:

exten => 220,1,Answer
exten => 220,n,MusicOnHold()
exten => 220,1,Answer()
exten => 220,n,StartMusicOnHold()
exten => 220,n,Wait(10)
exten => 220,n,StopMusicOnHold()

In brackets, you can specify the class and duration of the music on hold, for example MusicOnHold(default,60)

You can also specify music on hold directly in the Dial command:

exten => 220,1,Dial(SIP/220,30,m(newclass))

After changing the configuration, restart asterisk:

service asterisk restart

Or, without restarting asterisk, tell it to re-read the specific configuration, for example musiconhold.conf and extensions.conf:

asterisk -rvv
moh reload
dialplan reload
exit

See also my articles:
How to convert audio files to ulaw, alaw, gsm, g722, etc. for Asterisk
My other articles about Asterisk

Leave a comment

Leave a Reply