mediaDeviceBtn.addEventListener("click", async () => {
if ("setSinkId" in AudioContext.prototype) {
selectDiv.textContent = "";
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const devices = await navigator.mediaDevices.enumerateDevices();
// Most of the DOM scripting to generate the dropdown cut out for brevity
const audioOutputs = devices.filter(
(device) =>
device.kind === "audiooutput" && device.deviceId !== "default",
);
audioOutputs.forEach((device) => {
const option = document.createElement("option");
option.value = device.deviceId;
option.textContent = device.label;
select.appendChild(option);
});
const option = document.createElement("option");
option.value = "none";
option.textContent = "None";
select.appendChild(option);
select.addEventListener("change", async () => {
if (select.value === "none") {
await audioCtx.setSinkId({ type: "none" });
} else {
await audioCtx.setSinkId(select.value);
}
});
}
});AudioContext: setSinkId() method / notebook
AudioContext: setSinkId() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.