From 5aa69c917f3c39959c17a859227e5d7d47248ccd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 24 Nov 2023 23:18:15 -0600 Subject: [PATCH] Gameboy: make mute button work properly --- src/components/gameboy.tsx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/components/gameboy.tsx b/src/components/gameboy.tsx index 457aeb74cf..bc8197fd1c 100644 --- a/src/components/gameboy.tsx +++ b/src/components/gameboy.tsx @@ -7,6 +7,8 @@ import { exitFullscreen, isFullscreen, requestFullscreen } from 'soapbox/feature import { IconButton } from './ui'; +let gainNode: GainNode | undefined; + interface IGameboy extends Pick, 'onFocus' | 'onBlur'> { /** Classname of the outer `
`. */ className?: string; @@ -65,11 +67,7 @@ const Gameboy: React.FC = ({ className, src, aspect = 'normal', onFocu }; const togglePaused = () => paused ? play() : pause(); - - const unmute = async () => { - await WasmBoy.resumeAudioContext(); - setMuted(false); - }; + const toggleMuted = () => setMuted(!muted); const toggleFullscreen = () => { if (isFullscreen()) { @@ -101,6 +99,12 @@ const Gameboy: React.FC = ({ className, src, aspect = 'normal', onFocu } }, [fullscreen]); + useEffect(() => { + if (gainNode) { + gainNode.gain.value = muted ? 0 : 1; + } + }, [gainNode, muted]); + return (
= ({ className, src, aspect = 'normal', onFocu /> { + gainNode = gainNode ?? audioContext.createGain(); + audioBufferSourceNode.connect(gainNode); + return gainNode; + }, saveStateCallback: false, };