Guard against undefined AudioContext in useAudioContext hook (#39397)
This commit is contained in:
parent
ce8bf016bd
commit
f79e0109fe
@ -18,7 +18,7 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => {
|
|||||||
const gainNodeRef = useRef<GainNode>();
|
const gainNodeRef = useRef<GainNode>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!audioElementRef.current) {
|
if (!audioElementRef.current || typeof AudioContext === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,13 +43,17 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => {
|
|||||||
}, [audioElementRef]);
|
}, [audioElementRef]);
|
||||||
|
|
||||||
const playAudio = useCallback(() => {
|
const playAudio = useCallback(() => {
|
||||||
void audioElementRef.current?.play();
|
if (audioContextRef.current && audioElementRef.current) {
|
||||||
void audioContextRef.current?.resume();
|
void audioElementRef.current.play();
|
||||||
|
void audioContextRef.current.resume();
|
||||||
|
}
|
||||||
}, [audioElementRef]);
|
}, [audioElementRef]);
|
||||||
|
|
||||||
const pauseAudio = useCallback(() => {
|
const pauseAudio = useCallback(() => {
|
||||||
audioElementRef.current?.pause();
|
if (audioContextRef.current && audioElementRef.current) {
|
||||||
void audioContextRef.current?.suspend();
|
audioElementRef.current.pause();
|
||||||
|
void audioContextRef.current.suspend();
|
||||||
|
}
|
||||||
}, [audioElementRef]);
|
}, [audioElementRef]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user