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 9359b0b88e..fcd4593071 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -72,7 +72,7 @@ export class AudioSinkManager { */ async unblockAutoplay() { if (this.autoPausedTracks.size > 0) { - this.unpauseAudioTracks(); + await this.unpauseAudioTracks(); } } diff --git a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts index 75100bb9a5..93f378890d 100644 --- a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts +++ b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts @@ -4,17 +4,13 @@ import AnalyticsEventFactory from '../../analytics/AnalyticsEventFactory'; import { ErrorFactory } from '../../error/ErrorFactory'; import { HMSAction } from '../../error/HMSAction'; import { EventBus } from '../../events/EventBus'; +import { HMSAudioContextHandler } from '../../internal'; import { HMSLocalAudioTrack } from '../../media/tracks'; import Room from '../../sdk/models/HMSRoom'; import HMSLogger from '../../utils/logger'; const DEFAULT_SAMPLE_RATE = 48000; -//Handling sample rate error in case of firefox -const checkBrowserSupport = () => { - return navigator.userAgent.indexOf('Firefox') !== -1; -}; - /** * This class manages applying different plugins on a local audio track. Plugins which need to modify the audio * are called in the order they were added. Plugins which do not need to modify the audio are called @@ -48,7 +44,7 @@ export class HMSAudioPluginsManager { this.hmsTrack = track; this.pluginsMap = new Map(); this.analytics = new AudioPluginsAnalytics(eventBus); - this.createAudioContext(); + this.audioContext = HMSAudioContextHandler.getAudioContext({ sampleRate: DEFAULT_SAMPLE_RATE }); this.room = room; } @@ -214,7 +210,7 @@ export class HMSAudioPluginsManager { for (const plugin of plugins) { await this.addPlugin(plugin); } - this.updateProcessedTrack(); + await this.updateProcessedTrack(); } private async initAudioNodes() { @@ -282,19 +278,4 @@ export class HMSAudioPluginsManager { plugin.stop(); this.analytics.removed(name); } - - private createAudioContext() { - if (!this.audioContext) { - if (checkBrowserSupport()) { - /** - Not setting default sample rate for firefox since connecting - audio nodes from context with different sample rate is not - supported in firefox - */ - this.audioContext = new AudioContext(); - } else { - this.audioContext = new AudioContext({ sampleRate: DEFAULT_SAMPLE_RATE }); - } - } - } } diff --git a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts index 9b880b70f7..2d82826750 100644 --- a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts +++ b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts @@ -1534,24 +1534,29 @@ export class HMSSDKActions { export interface HMSAudioContext { audioContext: AudioContext | null; - getAudioContext: () => AudioContext; + getAudioContext: (options?: AudioContextOptions) => AudioContext; resumeContext: () => Promise; } export const HMSAudioContextHandler: HMSAudioContext = { audioContext: null, - getAudioContext() { - if (!this.audioContext) { - this.audioContext = new AudioContext(); + getAudioContext(options?: AudioContextOptions) { + const newAudioContextNeeded = + !this.audioContext || (options?.sampleRate && this.audioContext.sampleRate !== options.sampleRate); + + if (newAudioContextNeeded) { + /** + * Not setting default sample rate for firefox since connecting + * audio nodes from context with different sample rate is not + * supported in firefox + */ + this.audioContext = isFirefox ? new AudioContext() : new AudioContext(options); } - return this.audioContext; + + return this.audioContext!; }, async resumeContext() { try { diff --git a/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx b/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx index ecd2a93f98..a8ba68b958 100644 --- a/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx +++ b/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx @@ -9,9 +9,9 @@ export function AutoplayBlockedModal() { return ( { + onOpenChange={async value => { if (!value) { - unblockAudio(); + await unblockAudio(); } resetError(); }} @@ -25,8 +25,8 @@ export function AutoplayBlockedModal() {