mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Route voice playback through a hidden <audio> element for device switching
AudioContext.setSinkId only exists in Chrome; HTMLMediaElement.setSinkId is supported more broadly (including Firefox 130+). Playback now goes through a MediaStreamAudioDestinationNode into a hidden <audio> element so output device switching actually takes effect outside Chrome/Edge.
This commit is contained in:
1 parent
b3e55a60b5
commit
a5c1ae62ff
2 files changed
+48
-16
No files matched your search
+3
-3
@@ -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 () => {
|
||||
|
||||
+45
-13
@@ -140,22 +140,34 @@ export async function pickAudioOutputDevice(): Promise<MediaDeviceInfo | null> {
|
||||
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<void> {
|
||||
const ctx = context as AudioContext & { setSinkId?: (id: string) => Promise<void> };
|
||||
if (typeof ctx.setSinkId !== "function") return;
|
||||
await ctx.setSinkId(deviceId);
|
||||
}
|
||||
type SinkableElement = HTMLAudioElement & { setSinkId?: (id: string) => Promise<void> };
|
||||
type SinkableContext = AudioContext & { setSinkId?: (id: string) => Promise<void> };
|
||||
|
||||
/** 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 `<audio>` element rather than straight to `context.destination`,
|
||||
* because device switching support is split across browsers:
|
||||
* `AudioContext.setSinkId` (Chrome 110+) only retargets the context itself,
|
||||
* while `HTMLMediaElement.setSinkId` (Chrome, and Firefox 130+) only works on
|
||||
* a media element. Routing through the element lets `setOutputDevice` use
|
||||
* whichever one the browser actually supports.
|
||||
*/
|
||||
export class AudioPlayer {
|
||||
private nextTime = 0;
|
||||
private destination: MediaStreamAudioDestinationNode;
|
||||
private element: SinkableElement;
|
||||
|
||||
constructor(private context: AudioContext) {}
|
||||
constructor(private context: AudioContext) {
|
||||
this.destination = context.createMediaStreamDestination();
|
||||
this.element = document.createElement("audio") as SinkableElement;
|
||||
this.element.autoplay = true;
|
||||
this.element.srcObject = this.destination.stream;
|
||||
this.element.style.display = "none";
|
||||
document.body.appendChild(this.element);
|
||||
}
|
||||
|
||||
playFrame(base64Pcm: string): void {
|
||||
const int16 = base64ToInt16(base64Pcm);
|
||||
@@ -172,7 +184,7 @@ export class AudioPlayer {
|
||||
|
||||
const source = this.context.createBufferSource();
|
||||
source.buffer = buffer;
|
||||
source.connect(this.context.destination);
|
||||
source.connect(this.destination);
|
||||
|
||||
const now = this.context.currentTime;
|
||||
if (this.nextTime < now + 0.02) {
|
||||
@@ -187,4 +199,24 @@ export class AudioPlayer {
|
||||
reset(): void {
|
||||
this.nextTime = 0;
|
||||
}
|
||||
|
||||
/** Routes playback to a specific device (empty string = system default). */
|
||||
async setOutputDevice(deviceId: string): Promise<void> {
|
||||
if (typeof this.element.setSinkId === "function") {
|
||||
await this.element.setSinkId(deviceId);
|
||||
return;
|
||||
}
|
||||
const ctx = this.context as SinkableContext;
|
||||
if (typeof ctx.setSinkId === "function") {
|
||||
await ctx.setSinkId(deviceId);
|
||||
}
|
||||
// Neither API is available - stays on the system default output.
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.element.pause();
|
||||
this.element.srcObject = null;
|
||||
this.element.remove();
|
||||
this.destination.stream.getTracks().forEach((t) => t.stop());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user