diff --git a/change-beta/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json b/change-beta/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json new file mode 100644 index 00000000000..4d04bfc8ecb --- /dev/null +++ b/change-beta/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json @@ -0,0 +1,9 @@ +{ + "type": "patch", + "area": "fix", + "workstream": "Audio Context", + "comment": "Fix issue where AudioContext for composites is made too early", + "packageName": "@azure/communication-react", + "email": "dmceachern@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/change/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json b/change/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json new file mode 100644 index 00000000000..4d04bfc8ecb --- /dev/null +++ b/change/@azure-communication-react-e0efc724-fb57-4670-a7dd-91417bb601da.json @@ -0,0 +1,9 @@ +{ + "type": "patch", + "area": "fix", + "workstream": "Audio Context", + "comment": "Fix issue where AudioContext for composites is made too early", + "packageName": "@azure/communication-react", + "email": "dmceachern@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/packages/react-composites/src/composites/common/AudioProvider.tsx b/packages/react-composites/src/composites/common/AudioProvider.tsx index 53ab21b6046..b311a8cfe9c 100644 --- a/packages/react-composites/src/composites/common/AudioProvider.tsx +++ b/packages/react-composites/src/composites/common/AudioProvider.tsx @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import React, { useContext, createContext } from 'react'; +import React, { useContext, createContext, useState, useEffect } from 'react'; /** * @private @@ -18,19 +18,26 @@ export interface ACSAudioProviderProps { */ export const ACSAudioProvider = (props: ACSAudioProviderProps): JSX.Element => { const { audioContext, children } = props; + const [stateAudioContext, setStateAudioContext] = useState(undefined); + + useEffect(() => { + // Create the AudioContext only when the component is rendered + setStateAudioContext(audioContext); + }, [audioContext]); + const alreadyWrapped = useAudio(); if (alreadyWrapped) { return <>{children}; } - return {props.children}; + return {props.children}; }; /** * @private */ -const ACSAudioContext = createContext(new AudioContext()); +const ACSAudioContext = createContext(undefined); /** * @private */ -export const useAudio = (): AudioContext => useContext(ACSAudioContext); +export const useAudio = (): AudioContext | undefined => useContext(ACSAudioContext);