如何使用 Web Audio select 目标输出设备 Api
How to select destination output device using Web Audio Api
我一直在使用网络音频 api 并创建了一个上下文,并用数据填充了一个源缓冲区。它在默认输出设备上播放良好,但我不明白如何选择目的地。在旧的 w3 规范中,您可以将正确的 deviceId 传递给音频上下文构造函数,但我现在不知道如何在不使用媒体元素的情况下执行此操作。有什么建议吗?
source = context.createBufferSource()
source.loop = true;
source.buffer = globalAudioBuffer;
source.connect(context.destination);
context.resume();
source.start(0);
遗憾的是,尚未实现设置网络音频图的目标音频设备,并且 api 尚未最终确定。
您现在可以做的是将网络音频图连接到 HTML 元素,并且 set the sinkid of the element(目前仅适用于 Chrome)
这是一个简单的例子:
var ac = new AudioContext();
var audio = new Audio();
var o = ac.createOscillator();
o.start();
var dest = ac.createMediaStreamDestination();
o.connect(dest);
audio.src = URL.createObjectURL(dest.stream);
audio.play();
现在您的振荡器将通过音频元素播放并且
您现在可以使用连接的输出设备的 deviceId 调用 audio.setSinkId()
。
适用于 Chrome 100,但不适用于 Firefox 98 的完整示例。
因为 的另一个答案在这里不起作用(对 createObjectURL
的调用无效)并且因为另一个答案没有显示 setSinkId()
,所以在这里发布用法。
下面的代码片段在系统 (audioDevices[2]
) 上找到的第三个音频输出设备上播放音频。
在 JSFiddle 试试看。
async function playAudio() {
await navigator.mediaDevices.getUserMedia({
audio: { deviceId: undefined },
video: false
});
const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevices = devices.filter((device) => {
return (
device.kind === "audiooutput"
);
});
const audioDevice = audioDevices[2];
var audioContext = new AudioContext();
var audioElement = new Audio();
await audioElement.setSinkId(audioDevice.deviceId);
var oscillator = audioContext.createOscillator();
var mediaStreamDestination = audioContext.createMediaStreamDestination();
oscillator.connect(mediaStreamDestination);
audioElement.srcObject = mediaStreamDestination.stream;
oscillator.start();
audioElement.play();
await new Promise((r) => setTimeout(r, 2000));
oscillator.stop();
}
playAudio();
我一直在使用网络音频 api 并创建了一个上下文,并用数据填充了一个源缓冲区。它在默认输出设备上播放良好,但我不明白如何选择目的地。在旧的 w3 规范中,您可以将正确的 deviceId 传递给音频上下文构造函数,但我现在不知道如何在不使用媒体元素的情况下执行此操作。有什么建议吗?
source = context.createBufferSource()
source.loop = true;
source.buffer = globalAudioBuffer;
source.connect(context.destination);
context.resume();
source.start(0);
遗憾的是,尚未实现设置网络音频图的目标音频设备,并且 api 尚未最终确定。
您现在可以做的是将网络音频图连接到 HTML 元素,并且 set the sinkid of the element(目前仅适用于 Chrome)
这是一个简单的例子:
var ac = new AudioContext();
var audio = new Audio();
var o = ac.createOscillator();
o.start();
var dest = ac.createMediaStreamDestination();
o.connect(dest);
audio.src = URL.createObjectURL(dest.stream);
audio.play();
现在您的振荡器将通过音频元素播放并且
您现在可以使用连接的输出设备的 deviceId 调用 audio.setSinkId()
。
适用于 Chrome 100,但不适用于 Firefox 98 的完整示例。
因为 createObjectURL
的调用无效)并且因为另一个答案没有显示 setSinkId()
,所以在这里发布用法。
下面的代码片段在系统 (audioDevices[2]
) 上找到的第三个音频输出设备上播放音频。
在 JSFiddle 试试看。
async function playAudio() {
await navigator.mediaDevices.getUserMedia({
audio: { deviceId: undefined },
video: false
});
const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevices = devices.filter((device) => {
return (
device.kind === "audiooutput"
);
});
const audioDevice = audioDevices[2];
var audioContext = new AudioContext();
var audioElement = new Audio();
await audioElement.setSinkId(audioDevice.deviceId);
var oscillator = audioContext.createOscillator();
var mediaStreamDestination = audioContext.createMediaStreamDestination();
oscillator.connect(mediaStreamDestination);
audioElement.srcObject = mediaStreamDestination.stream;
oscillator.start();
audioElement.play();
await new Promise((r) => setTimeout(r, 2000));
oscillator.stop();
}
playAudio();