Skip to content

Commit

Permalink
[Bugfix] update Audio context creation (#5476)
Browse files Browse the repository at this point in the history
* update context creation

* Change files

* fix audio test issue
  • Loading branch information
dmceachernmsft authored Dec 9, 2024
1 parent be06765 commit 9b4a08c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}
15 changes: 11 additions & 4 deletions packages/react-composites/src/composites/common/AudioProvider.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,19 +18,26 @@ export interface ACSAudioProviderProps {
*/
export const ACSAudioProvider = (props: ACSAudioProviderProps): JSX.Element => {
const { audioContext, children } = props;
const [stateAudioContext, setStateAudioContext] = useState<AudioContext | undefined>(undefined);

useEffect(() => {
// Create the AudioContext only when the component is rendered
setStateAudioContext(audioContext);
}, [audioContext]);

const alreadyWrapped = useAudio();
if (alreadyWrapped) {
return <>{children}</>;
}
return <ACSAudioContext.Provider value={audioContext}>{props.children}</ACSAudioContext.Provider>;
return <ACSAudioContext.Provider value={stateAudioContext}>{props.children}</ACSAudioContext.Provider>;
};

/**
* @private
*/
const ACSAudioContext = createContext<AudioContext>(new AudioContext());
const ACSAudioContext = createContext<AudioContext | undefined>(undefined);

/**
* @private
*/
export const useAudio = (): AudioContext => useContext(ACSAudioContext);
export const useAudio = (): AudioContext | undefined => useContext(ACSAudioContext);

0 comments on commit 9b4a08c

Please # to comment.