Music on hold at Asterisk

Music on hold at Asterisk

Music On Hold (MOH) is a feature in the Asterisk telephone system that allows music to play in the background while a call is being held or transferred. One or many audio files can then be played. By default, the Asterisk installer adds audio files to the /var/lib/asterisk/moh directory and creates a default music class. We can add our own music classes and sound recordings. Files should be in WAV or MP3 format and have appropriate read and play permissions.

Table of Contents

1) Examples of using music on hold

In Asterisk, you can customize each situation in which different audio files are played. Below are some example situations:
  1. Music on hold – those audio files that are played when a call is transferred to the waiting queue.
  2. Ringback tone – these audio files are played while the caller is waiting for the call.
  3. Music on transfer – these audio files are played when the caller is transferred to another number or when a conference call occurs
In Asterisk you can configure custom music classes and adjust their behavior to suit your individual needs.

2) Musiconhold.conf file

The musiconhold.conf file is an Asterisk system configuration file that contains music class configurations for the Music on Hold (MoH) functionality. A music class is a collection of audio files that are played to the user when they are waiting for a call or being redirected. Sample file musiconhold.conf.sample is located in the github repository. After system installation, the musiconhold.conf file is located in the /etc/asterisk/ directory and consists of the [general] section and sections describing individual music classes. Each section should contain the name of the music class in square brackets, followed by a list of audio files to play. Sound files are separated by commas, can be downloaded from a specified folder or even streamed live. Here is an example configuration of the musiconhold.conf file:
					nano /etc/asterisk/musiconhold.conf



application=/usr/bin/mpg123 -q -r 8000 -f 8192 -b 2048 --mono -s

In this example, the musiconhold.conf file consists of three sections: general and describing two classes of music: default and mohmp3 In the [general] section there is actually only one usable cachertclasses parameter. The "yes" option means that Asterisk will use only one instance of the music class for all users who will use it. This will reduce CPU and memory usage, but the user may hear the music not necessarily from the beginning of the song. It will be played periodically as you use this class. The [default] section specifies that the “default” music class should use files stored in the moh directory. This indicates a subdirectory in the path /var/lib/asterisk/, i.e. /var/lib/asterisk/moh The "mode=files" parameter means that the sound files are saved in a format understandable by the Asterisk system. The additional parameter "sort=random" means that the sound files are played randomly. Other sorting options are: “alpha” and “randstart”. “alpha” means that files are sorted alphabetically. “randstart” means, the files are also sorted alphabetically, but the first file is selected randomly. The [mohmp3] section specifies that the “mohmp3” music class should use files stored in the /var/lib/asterisk/mohmp3 directory (directory parameter). The "mode=custom" parameter indicates the use of a dedicated music playback application. It is defined using the “application” parameter. We use the mpg123 application. You can install it by issuing the following command:
					# CentOS
sudo yum install mpg123

# Ubuntu
sudo apt-get install mpg123
After saving the changes to the musiconhold.conf file, it is necessary to execute the "module reload" command in the Asterisk console or restart the entire system for the changes to take effect.

3) SetMusicOnHold() function

SetMusicOnHold() is a function available in Asterisk that is used to set music classes while waiting for a connection. This function allows you to dynamically change the music class during a call. The syntax of the SetMusicOnHold() function is as follows:

The “class” parameter specifies the name of the music class to be played while waiting for a connection. This name must match one of the music classes configured in the Asterisk configuration file. Example of using the SetMusicOnHold() function in Asterisk Dialplan:
					nano /etc/asterisk/extensions.conf

					exten => 100,1,Answer()
 same => n,SetMusicOnHold(mohmp3)
 same => n,Dial(SIP/100,30)
 same => n,Hangup()

In this example, SetMusicOnHold() sets the music class to "mohmp3" while waiting for a connection, and then calls Dial() to establish the connection with the numberSIP/100. When the call ends, the Hangup() function ends the call. Instead of using the SetMusicOnHold()function, you can use the "m" parameter directly in the Dial() function. Our Dialplan could look like this:
					exten => 100,1,Answer()
 same => n,Dial(SIP/100,30,m(mohmp3))
 same => n,Hangup()

You don’t have to be a virtuoso to set up hold music on your Asterisk system. As you can see, this is not a complicated process, and it can make the customer experience more pleasant when connecting to your system. Of course, you can also experiment a bit and instead of files, you can start streaming music from an external service.
					application=/usr/bin/streamplayer 888
But more about that, maybe next time 😅
Do you know what is really happening on your PBX? Try our proprietary software VOIPERO. The system has recently been launched and is now available completely free of charge. Installation and configuration takes a few minutes Find out what the VOIPERO system has to offer when it comes to live reporting and monitoring VoIP systems based on Asterisk.
Share this post

Do you have questions or you need an offer?

Contact us!

Most popular

Related Posts

We Have Launched

Monitoring & Reporting of Your VoIP Server