diff --git a/web/src/App.tsx b/web/src/App.tsx index 876df3a..4a5a02f 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -7,7 +7,6 @@ import { hasNativeOutputPicker, listAudioOutputDevices, pickAudioOutputDevice, - setAudioOutputDevice, } from "./voice"; const GATEWAY_URL = "ws://localhost:8080"; @@ -293,9 +292,10 @@ function App() { socket.onclose = () => { setConnected(false); stopMic(); + audioPlayerRef.current?.dispose(); + audioPlayerRef.current = null; audioContextRef.current?.close(); audioContextRef.current = null; - audioPlayerRef.current = null; }; }; @@ -327,7 +327,7 @@ function App() { const handleOutputDeviceChange = async (deviceId: string, label?: string) => { setOutputDeviceId(deviceId); setOutputDeviceLabel(label || (deviceId ? `Output ${deviceId.slice(0, 6)}` : "System default")); - if (audioContextRef.current) await setAudioOutputDevice(audioContextRef.current, deviceId); + await audioPlayerRef.current?.setOutputDevice(deviceId); }; const handlePickOutputDevice = async () => { diff --git a/web/src/voice.ts b/web/src/voice.ts index c7d80b1..891f53d 100644 --- a/web/src/voice.ts +++ b/web/src/voice.ts @@ -140,22 +140,34 @@ export async function pickAudioOutputDevice(): Promise { return md.selectAudioOutput(); } -/** - * Routes an AudioContext's output to a specific device (empty string = system default). - * `setSinkId` on AudioContext is a fairly recent addition (Chrome 110+); older - * browsers simply keep using the system default output. - */ -export async function setAudioOutputDevice(context: AudioContext, deviceId: string): Promise { - const ctx = context as AudioContext & { setSinkId?: (id: string) => Promise }; - if (typeof ctx.setSinkId !== "function") return; - await ctx.setSinkId(deviceId); -} +type SinkableElement = HTMLAudioElement & { setSinkId?: (id: string) => Promise }; +type SinkableContext = AudioContext & { setSinkId?: (id: string) => Promise }; -/** Schedules incoming base64-encoded stereo 16-bit PCM frames for gap-free playback. */ +/** + * Schedules incoming base64-encoded stereo 16-bit PCM frames for gap-free + * playback, and supports switching output device across browsers. + * + * Playback is routed through a `MediaStreamAudioDestinationNode` into a + * hidden `