CometChat Calls SDK v5 — Audio Controls
Overview
Programmatically control the local microphone (mute/unmute) and switch audio input/output devices during an active call.
Key Imports
import { CometChatCalls } from '@cometchat/calls-sdk-javascript';
Implementation
Mute / Unmute
CometChatCalls.muteAudio(); // mute microphone
CometChatCalls.unmuteAudio(); // unmute microphone
CometChatCalls.toggleAudio(); // toggle mute state
Get Available Audio Devices
const audioInputs = CometChatCalls.getAudioInputDevices(); // microphones
const audioOutputs = CometChatCalls.getAudioOutputDevices(); // speakers/headphones
Get Current Device
const currentMic = CometChatCalls.getCurrentAudioInputDevice();
const currentSpeaker = CometChatCalls.getCurrentAudioOutputDevice();
Switch Audio Input (Microphone)
const devices = CometChatCalls.getAudioInputDevices();
CometChatCalls.setAudioInputDevice(devices[1].deviceId);
Switch Audio Output (Speaker)
const outputs = CometChatCalls.getAudioOutputDevices();
CometChatCalls.setAudioOutputDevice(outputs[0].deviceId);
Listen for Audio Events
CometChatCalls.addEventListener('onAudioMuted', () => {
// Update mute button to "muted" state
});
CometChatCalls.addEventListener('onAudioUnMuted', () => {
// Update mute button to "unmuted" state
});
CometChatCalls.addEventListener('onAudioInputDeviceChanged', (device) => {
console.log('Mic switched to:', device.label);
});
CometChatCalls.addEventListener('onAudioOutputDeviceChanged', (device) => {
console.log('Speaker switched to:', device.label);
});
CometChatCalls.addEventListener('onAudioInputDevicesChanged', (devices) => {
// Device list changed (plugged/unplugged) — rebuild UI
});
CometChatCalls.addEventListener('onAudioOutputDevicesChanged', (devices) => {
// Output device list changed — rebuild UI
});
Pre-select Device (Before Joining)
const sessionSettings: SessionSettings = {
audioInputDeviceId: 'specific-device-id',
audioOutputDeviceId: 'specific-output-id',
startAudioMuted: true, // join muted
};
Gotchas
muteAudio()/unmuteAudio()only work during an active session- Device lists return
MediaDeviceInfoobjects withdeviceId,label,kind,groupId setAudioOutputDevice()may not work in all browsers (Safari has limited support)- Device labels may be empty strings until the user grants microphone permission
- Use
onAudioInputDevicesChangedto detect when devices are plugged/unplugged - For voice calls, consider setting
startAudioMuted: falseas default