Perform Music on Hold

⌘K
  1. Home
  2. Programmable Voice
  3. How do I
  4. Perform Music on Hold

Perform Music on Hold

When creating a telephony application, you will often want to play music to your customers that are on hold. There are multiple ways to do this; two of the most common are described below.

Use a Separate ‘Music on Hold’ Thread

One of the more efficient ways is to set up a separate thread that continuously plays music to a Voice Resource. When you want to have customers listen to that music, you would then half-route them to the Voice Resource that is playing the music. You would then route them to a separate channel resource once the operator becomes available. You would start this up at application start up:

mohChannel = TelephonyServer.GetVoiceResource();

while(true)
{
     mohChannel.Play("moh.wav");
     if(stopPlaying)
     {
          // When you shut down the application, you will want to set the stop playing
          // variable to true. You will also want to call mohChannel.Stop()
          break;
     }
}

Since this VoiceResource is always playing MoH, it is only necessary to have the call’s ChannelResource listen to it via a half-route when needed:

m_ChannelResource.RouteHalf(mohChannel);
// The customer now hears the MoH
//When MoH is no longer needed, listen to the channel’s VoiceResource
m_ChannelResource.RouteHalf(m_VoiceChannel);

Use the Thread’s Assigned Voice Resource on Demand

In some environments it may not make sense to dedicate one (or more) voice resources to the purpose of playing music on hold. In these cases, it may be more appropriate to play music on hold using the thread’s already-assigned voice resource as needed.

// Application logic determines music on hold is needed; start MoH loop
while(playMoH)
{
     m_ChannelResource.VoiceResource.Play(“moh.wav”);
     // Application logic checks to see if MoH is still needed. If not, set playMoH
     // to false.
}

A disadvantage of this method is that, since the music on hold file is played synchronously, it will need to be played completely before the application can continue. For this reason, the file should be kept relatively short in duration to prevent unnecessary delays in further call processing

More Advanced Features

There are more advanced features that you can add. For example, you can randomly pick a wav file to play, or use short music snippets, and update the user with the approximate hold time. You could also have separate music on hold sources for different purposes, for example one for customers holding for sales, and a different one for support.

A Few Notes

When using a dedicated thread, or threads, for music on hold you may need to add additional voice resources so you don’t run out. This link describes how to do this.

Also, the examples above are just code snippets. You will want to add additional error handling using Try / Catch Blocks.

It’s also a great idea to put the Music on Hold thread in its own class; here’s an example implementation. The code below show’s how you might use this class:

private MusicOnHold moh=null;
string vFile = "Files\\RingMuLaw.wav";
// Create new MusicOnHold instance; “Log” is your application’s
// VoiceElements.Common.Log instance.
moh = new MusicOnHold(Log, m_TelephonyServer, m_VoiceResource, vFile);
moh.Start();
// Since MoH is threaded, logic immediately drops through here
// When ready to stop moh.Stop();

 

As always, don’t hesitate to contact support@inventivelabs.com for additional questions.

Was this article helpful to you? No Yes 14

How can we help?