AudioTrack replacement issue on device change


We are having an issue where when the user tries to change or connect to headphones while on call, the user gets disconnected from the session.

We also tried ondevicechange (navigator) to replace the audio track. However, replacing it to id ‘default’ does not seem to work either.

Is there a better solution to handle audio input device changes ? Find snippet below:

navigator.mediaDevices.ondevicechange = async (event) => {
    OV.getDevices().then(async (devices) => {
      const audioDevices = devices.filter((device) => device.kind === 'audioinput');

      const mediaStream = await OV.getUserMedia({
        audioSource: audioDevices[0].deviceId,

      const audioTrack = mediaStream.getAudioTracks()[0];
      if (audioTrack.readyState === 'live') {
          .catch((error) => console.error('Error replacing track', error));

Any help on this would be really helpful! Thanks in advance!