diff --git a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts index e61f53a60f..9359b0b88e 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -9,6 +9,7 @@ import { HMSRemoteAudioTrack } from '../media/tracks'; import { HMSRemotePeer } from '../sdk/models/peer'; import { Store } from '../sdk/store'; import HMSLogger from '../utils/logger'; +import { sleep } from '../utils/timer-utils'; /** * Following are the errors thrown when autoplay is blocked in different browsers @@ -137,11 +138,15 @@ export class AudioSinkManager { ); this.eventBus.analytics.publish(AnalyticsEventFactory.audioPlaybackError(ex)); if (audioEl?.error?.code === MediaError.MEDIA_ERR_DECODE) { - await track.setVolume(0); - await track.setVolume(this.volume); - this.eventBus.analytics.publish( - AnalyticsEventFactory.audioRecovered('Audio recovered after media decode error'), - ); + // try to wait for main execution to complete first + this.removeAudioElement(audioEl, track); + await sleep(500); + await this.handleTrackAdd({ track, peer, callListener: false }); + if (!this.state.autoplayFailed) { + this.eventBus.analytics.publish( + AnalyticsEventFactory.audioRecovered('Audio recovered after media decode error'), + ); + } } }; track.setAudioElement(audioEl); diff --git a/packages/hms-video-store/src/device-manager/DeviceManager.ts b/packages/hms-video-store/src/device-manager/DeviceManager.ts index 60939a00f2..3898560b06 100644 --- a/packages/hms-video-store/src/device-manager/DeviceManager.ts +++ b/packages/hms-video-store/src/device-manager/DeviceManager.ts @@ -483,7 +483,8 @@ export class DeviceManager implements HMSDeviceManager { */ // eslint-disable-next-line complexity public autoSelectAudioOutput = async () => { - if ('ondevicechange' in navigator.mediaDevices || !this.audioInputChanged) { + // do this only after join so the earpiece would be selected at the right time + if ('ondevicechange' in navigator.mediaDevices || !this.store.getLocalPeer()?.joinedAt) { return; } const { bluetoothDevice, earpiece, speakerPhone, wired } = this.categorizeAudioInputDevices();