gpt4 book ai didi

javascript - 媒体记录器 : Record from Multiple Microphones

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:52 25 4
gpt4 key购买 nike

我目前正在使用 MediaRecorder用于在应用程序内录制音频的 API。

有没有办法从多个输入设备(比如两个麦克风)进行录音?

我可以使用 mediaDevices.enumerateDevices() 获取设备列表,如 here 所示.

理想情况下,如果可能的话,我想选择两个或更多的设备进行录制。

这可能吗?

最佳答案

解决方案是请求多个 getUserMedia 并将流与 AudioContext 混合。

这是如何做到这一点。

const VIDEO_ID = "video_id";
const MIC_1_ID = "mic_1_id";
const MIC_2_ID = "mic_2_id";

// Request permission
navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(() => {
// Loop over all available video and audio devices (input and output)
navigator.mediaDevices.enumerateDevices().then((devices) => {
// Request exact devices
const requests = [
navigator.mediaDevices.getUserMedia({
video: { deviceId: { exact: VIDEO_ID } },
}),
navigator.mediaDevices.getUserMedia({
video: false,
audio: { deviceId: { exact: MIC_1_ID } },
}),
navigator.mediaDevices.getUserMedia({
video: false,
audio: { deviceId: { exact: MIC_2_ID } },
}),
];

// Wait for all device requests to resolve.
Promise.all(requests).then((streams) => {
const ctx = new AudioContext();
const dest = ctx.createMediaStreamDestination();

// Connect streams to the destination audio stream.
streams.map((stream) => {
ctx.createMediaStreamSource(stream).connect(dest);
});

const videoTrack = streams[0].getTracks()[0];
const mixedTracks = dest.stream.getTracks()[0];

// Combine video and audio tracks into single stream.
const stream = new MediaStream([videoTrack, mixedTracks]);

// Start recorder
const recorder = new MediaRecorder(stream, { mimeType: "video/mp4" });

// ...
});
});
});

关于javascript - 媒体记录器 : Record from Multiple Microphones,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47059324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com