Adding Mute Unmute Functionality to the Twilio Voice JS SDK

July 24, 2023
Written by
Twilion
Reviewed by
Twilion
Twilion
Twilion

Mute Unmute Voice SDK Hero

In today's world of real-time communication, the ability to control audio is essential. Whether you're building a voice calling application, a conference bridge, or any other voice-enabled solution, having the ability to mute and unmute participants is a key functionality. In this article, I will show you how to effectively implement mute and unmute functionality using the Twilio Voice SDK.

We will dive into the technical aspects of muting and unmuting participants and discuss the necessary steps to integrate this feature seamlessly into your application. Whether you're a beginner or an experienced developer, this article will provide you with the knowledge and tools you need to empower your voice-enabled applications with mute and unmute functionality. You won’t learn how to build a voice app from scratch, but you will learn how to implement muting and unmuting quickly in your own app.

Prerequisites

In order to follow along, you’ll need:

Once you have the prerequisites completed and a running Voice application, we’re ready to begin the tutorial. Let’s get started!

Mute and Unmute Voice SDK

To connect the "mute" event listener with a mute button, follow these steps:

1. Add an HTML button element for the mute button in your HTML code. Give it an id attribute of button-mute-unmute to reference it in JavaScript:

<button id="button-mute-unmute" type="button">Mute</button> 

Make sure to place this code within the appropriate location in your HTML file, depending on where you want the button to appear. If you are following the Twilio Voice JavaScript SDK Quickstart, add this code beneath line 33 within the /public/index.html file.

2. In your JavaScript code, retrieve the mute button element using the getElementById:

const muteButton = document.getElementById("button-mute-unmute"); 

3. Add a click event listener to the mute button:

muteButton.addEventListener("click", () => {
  // Toggle mute state or perform actions when the mute button is clicked
});

4. Inside the click event listener, access the call object and use the call.mute() method to toggle the mute state of the call:

muteButton.addEventListener("click", () => {
  if (call && call.isMuted()) {
    // Unmute the call
    call.mute(false);
    console.log("Call Unmuted");
  } else {
    // Mute the call
    call.mute(true);
    console.log("Call Muted");
  }
});

5. Save the updated state of the call's mute status and update the mute button's text accordingly.

Make sure you have a call object available in the scope where you are adding the event listener. This object should represent the active call to which the mute functionality is applied.

When you navigate to localhost:3000, you should see the web application containing a Start up the Device button. Click this button to initialize a Twilio.Device. After the Twilio.Device is initialized, enter the number in E.164 format to make an outbound call from the Twilio Voice SDK and press the Call button.

Mute (and unmute) button added to the JavaScript Voice SDK

You should be able to see the Mute button (when the call is muted, it will become an Unmute button) in the Voice SDK client, and once the call is established you should be able to mute/unmute the call in progress. To ensure that the code is functioning correctly, I have intentionally included a console log within the if… else block. This allows us to verify whether the call is being muted or unmuted when the button is pressed. Adding console logs is a helpful practice for troubleshooting, as it allows us to identify any potential issues and determine which part of the code may be causing them.

Here is a snippet showing the console logs for reference:

Twilio Voice SDK console logs with muting and unmuting

Conclusion

In this article, you learned about the code that allows users to enable and disable audio in your JavaScript Voice SDK app powered by Twilio Programmable Voice. Before you go, try using the methods and events available for a call or learn how to generate an access token for your voice app in the backend.

Khushbu Shaikh is a dedicated Lead Technical Account Manager who is an invaluable asset to the personalized support team. With a wealth of experience, Khushbu excels in working with numerous accounts, diligently assisting them in overcoming challenges, and providing effective solutions. Her expertise lies in troubleshooting customer issues, offering insightful workarounds, and delivering exceptional support. For any inquiries or assistance, Khushbu can be reached at kshaikh [at] twilio.com.