Twilio error 31402 is an audio input device error that fires after microphone permission has been granted but the specific audio input device the SDK is attempting to use becomes unavailable or produces an error during stream acquisition or mid-call. This distinguishes it from 31201 (cannot acquire any microphone) and 31208 (permission denied): 31402 means a specific device that was working or was expected to work has failed at the hardware or driver level. The fix involves device enumeration and fallback selection.
What Causes This Error
A USB or Bluetooth headset that was disconnected while a call was in progress leaves the SDK attempting to read from a device ID that no longer exists, which triggers a NotReadableError on the audio track that the SDK maps to 31402. The operating system switching the default audio input device while the SDK has an active media stream can cause the SDK's currently acquired device to stop producing audio or to report an error, particularly on Windows where device priority changes can occur when a new audio device is connected. Audio driver crashes or hardware malfunctions that occur mid-call cause the audio track to fire an 'ended' event, which the SDK interprets as a 31402 device error. On corporate devices where IT policies restrict which audio devices are accessible to specific applications, a device that appears in the device enumeration list may produce a NotAllowedError when the SDK attempts to open a stream from it, which surfaces as 31402.
How to Fix It Step by Step
In your Device error handler for code 31402, call navigator.mediaDevices.enumerateDevices() to get the current list of available audio input devices and check whether the previously selected device still appears in the list: if it does not, the device has been physically disconnected. Present the user with a device selection UI that lists available microphones by label (returned from enumerateDevices) and allows them to select an alternative device, then call Device.audio.setInputDevice(deviceId) with the selected device ID to switch the SDK to the new input device. If automatic fallback is preferred over a UI prompt, select the first available audioinput device from the enumerateDevices result and call Device.audio.setInputDevice(deviceId) automatically, displaying a notification that the microphone was switched due to a device error. Listen to navigator.mediaDevices.addEventListener('devicechange') throughout the application session and update your available device list in real time, pre-selecting a fallback device so that a 31402 triggers an immediate automatic switch rather than dropping the call.
How to Prevent It from Recurring
Implement the devicechange event listener in your application from session start and maintain a live list of available audio input devices, displaying the current device selection prominently in the UI so users can see which microphone is active and switch before a failure occurs. Use the Voice SDK's Device.audio API to explicitly select a specific input device at call start using Device.audio.setInputDevice(deviceId), rather than relying on the browser's default selection, which gives your application control over which device is in use and allows programmatic fallback. Add a pre-call audio device test that plays a short tone and checks the audio level on the selected input device using an AudioContext and an AnalyserNode to confirm the device is producing audio signal before the call is connected, catching silent or failed devices before they generate a mid-call 31402. Build a call recovery flow that listens to Call.on('error') for 31402 and automatically attempts to reconnect using an alternative audio device from the available device list, minimizing call disruption when a device failure occurs.
When to Call a Specialist
If 31402 errors are occurring consistently on specific hardware or operating system combinations (for example, only on Windows 11 with specific USB audio chipsets), a specialist can investigate whether a known audio driver or Windows audio subsystem issue is causing the failure and recommend a driver update, OS setting change, or SDK configuration workaround. For enterprise deployments with IT-managed audio policies, a specialist can work with the IT team to identify which policy is restricting device access and configure the appropriate exceptions for your application's origin. You should also seek specialist help if you need to implement a professional-grade audio device management system with device health monitoring, automatic fallback, and call quality reporting, which requires deep integration with the Web Audio API and the Voice SDK's audio management layer.
Conclusion
Error 31402 is an audio input device failure that is handled by implementing device change listeners, automatic fallback device selection, and a device switch UI that allows users to recover without dropping the call. If this error is blocking your production system, contact our team and we will diagnose and fix it within the hour.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.