Music on Hold Configuration
Music on Hold Configuration
Music on Hold (MOH) plays audio to callers when they are placed on hold, waiting in a queueAn Asterisk call queue (app_queue) that parks incoming calls and distributes them to logged-in agents according to a chosen ring strategy., or parked. Asterisk supports multiple MOH classes, each pointing to a different directory of audio files.
MOH Class Definition (musiconhold.conf)
[default]
mode = files
directory = moh
sort = random
[company]
mode = files
directory = company/mohmp3
sort = random
[queue-sales]
mode = files
directory = custom/sales-moh
sort = alpha
Directory Setup
# Default MOH directory (usually pre-populated)
ls /var/lib/asterisk/moh/
# Create custom MOH directories
mkdir -p /var/lib/asterisk/company/mohmp3
mkdir -p /var/lib/asterisk/custom/sales-moh
# Upload audio files (WAV, MP3, or other supported formats)
cp company-jingle.wav /var/lib/asterisk/company/mohmp3/
cp sales-promo-1.wav /var/lib/asterisk/custom/sales-moh/
cp sales-promo-2.wav /var/lib/asterisk/custom/sales-moh/
Assigning MOH to Endpoints (pjsip.conf)
[1001](endpoint-internal)
auth = 1001-auth
aors = 1001
callerid = Alice Smith <1001>
moh_suggest = company
Using MOH in Dialplan
[queues]
; Queue with custom MOH class
exten => 100,1,Answer()
same => n,Set(CHANNEL(musicclass)=queue-sales)
same => n,Queue(sales,,,,120)
same => n,Hangup()
; Explicit MOH during a hold/transfer
exten => 200,1,Answer()
same => n,Set(CHANNEL(musicclass)=company)
same => n,Dial(PJSIP/1001,30)
same => n,Hangup()
Verify and Reload
*CLI> moh reload
*CLI> moh show classes
*CLI> moh show files
How it works
- mode = files: Asterisk reads audio files from the specified directory. This is the simplest and most common mode. Other modes include
custom(pipe audio from an external command likempg123) andmp3nb(non-blocking MP3 streaming). - directory: Relative paths are resolved from
/var/lib/asterisk/. Sodirectory = mohmeans/var/lib/asterisk/moh/. You can also use absolute paths. - sort:
randomshuffles the playlist each time.alphaplays files in alphabetical order.randstartstarts at a random position but plays sequentially from there. - moh_suggest: Set on a PJSIPThe modern SIP channel driver in Asterisk (chan_pjsip), replacing the older chan_sip. Configured in pjsip.conf using endpoints, AORs, auths, and transports. endpointIn PJSIP configuration, the SIP entity Asterisk communicates with: a phone, trunk, or other user agent. Defined by an endpoint section in pjsip.conf. to specify which MOH class the caller hears when this endpoint puts them on hold. The phone sends a re-INVITE with
sendonlySDP, and Asterisk starts playing the suggested MOH class. - CHANNELA single call leg passing through Asterisk. Channels represent connections to endpoints and are what dialplan applications act on.(musicclass): Overrides the MOH class for the current channel in the dialplanThe core call-routing configuration of Asterisk, written mostly in extensions.conf as contexts, extensions, and priorities that decide how every call is handled. Full definition →. This takes priority over
moh_suggestfrom the endpoint.
Tips
- At least one audio file must exist in the directory or the MOH class will fail to load. Check with
moh show files. - Convert audio to Asterisk-native format for best performance:
sox input.mp3 -r 8000 -c 1 output.wav. - For streaming internet radio, use
mode = customwithapplication = /usr/bin/mpg123 -q -s --mono -r 8000 http://stream-url. - The
defaultclass is used when no specific class is requested. Always keep it populated. - Queue MOH is set with the
musicclassparameter inqueues.conf, or by settingCHANNEL(musicclass)before callingQueue()in the dialplanThe core call-routing configuration of Asterisk, written mostly in extensions.conf as contexts, extensions, and priorities that decide how every call is handled. Full definition →. - Test MOH from the CLI:
channel originate PJSIP/1001 application MusicOnHold companyplays the "company" class to extensionA dialplan entry that matches a dialed number or pattern within a context and triggers a sequence of prioritized steps. 1001.
User Notes
Know a tip or gotcha for this topic? Share it below and help others.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.