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 queue, 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 PJSIP endpoint 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. - CHANNEL(musicclass): Overrides the MOH class for the current channel in the dialplan. 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 dialplan. - Test MOH from the CLI:
channel originate PJSIP/1001 application MusicOnHold companyplays the "company" class to extension 1001.
User Notes
No notes yet. Be the first to contribute a tip or example.
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.