answer call and play hold music

Debajyoti Sarma 0 Reputation points
2025-08-07T12:00:42.82+00:00

We are using Call Automation and Job Router together to manage our call jobs.

When we get Microsoft.Communication.IncomingCall event from azure event grid webhook, we do following

  1. Answer the call
       const { callConnection } = await callAutomationClient.answerCall(incomingCallContext, ACS_EVENT_GRID_BASE_URL);
       const { callConnectionId } = callConnection;
    
  2. sleep for 5s
       await new Promise((resolve) => setTimeout(resolve, 5000));
    
  3. play hold music
       const callConnection = await callAutomationClient.getCallConnection(callConnectionId);
           
       const callConnectionProperties = await  callConnection.getCallConnectionProperties();
           
       const playSource = {
             url: audioFileUrl,
             kind: "fileSource"
       };
       
       // Configure play options to loop the audio
       const playOptions = {
             loop: true
       };
       
       await callConnection
             .getCallMedia()
             .play([playSource], [ callConnectionProperties.source ], playOptions);
       
    

If we run Step 1 and 3 consecutively without sleep then getCallConnection is not able to find the callConnection. I think azure takes time to distribute the info about the call, so a sleep of 5s is needed.

We even tried to reuse callConnection object but that also gives problem in getCallConnectionProperties

Due to this after first ring on the incoming call, we have to wait for around 10-15s to listen to hold music of audioFileUrl.

Questions

  1. Is there a way to optimize this by removing sleep? Relying on another event to detect call answered is not an option because we are doing further processing in IncomingCall event.
  2. Is there a way to pass audioFileUrl while answering the call?
Azure Communication Services
{count} votes

1 answer

Sort by: Most helpful
  1. Debajyoti Sarma 0 Reputation points
    2025-08-12T10:07:42.6633333+00:00

    Hi,

    We will not be able to move to CallConnected event because we utilize from and to field of IncomingCall event to queue the call in job router queue after playing hold music. These fields are not available in CallConnected event. If we play hold music in CallConnected and queue the call in IncomingCall event, then both of these happens independently causing inconsistent state intermittently - hold music get played after call is attended by an agent. Overcoming this will need lot more complicate implementation.

    We have tried this approach earlier and observed that time between first ring and hold music doesn't reduce more that 2-3 seconds. Right now it's around 15s which get reduced to 12s.

    Is there a way to pass hold music while answering the call itself?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.