在 Firefox 的 MediaStream 问题中获取无音轨

Getting No audio tracks in MediaStream issue in firefox

我正在使用 getDisplayMedia 和 getUserMedia 捕获用户屏幕和音频,并且能够记录完整的屏幕捕获。但这仅适用于 Chrome 而不适用于 Firefox。当我 运行 我的应用程序在 Firefox 上时它抛出错误 'DOMException: AudioContext.createMediaStreamSource: No audio tracks in MediaStream'。下面是我的代码片段。我安装了这两种浏览器的最新版本。任何帮助,将不胜感激。提前致谢。

注意:- 它在 context.createMediaStreamSource(desktopStream)

行抛出错误
async function captureScreen() {
    desktopStream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: true });
    microPhoneStream = await navigator.mediaDevices.getUserMedia({ video: false, audio: true });

    const tracks = [
        ...desktopStream.getVideoTracks(),
        ...mergeAudioStreams(desktopStream,microPhoneStream)
    ];
    stream = new MediaStream(tracks);
    var options = { mimeType: "video/webm; codecs=opus,vp8" };
    startRecording(stream, options);
    ....
    ....
    ....
}


//merges two audio streams into one
const mergeAudioStreams = (desktopStream, microPhoneStream) => {
    const context = new AudioContext();
    try {
        const source1 = context.createMediaStreamSource(desktopStream);
        const source2 = context.createMediaStreamSource(microPhoneStream);
        const destination = context.createMediaStreamDestination();

        const desktopGain = context.createGain();
        const voiceGain = context.createGain();

        desktopGain.gain.value = 0.7;
        voiceGain.gain.value = 0.7;

        source1.connect(desktopGain).connect(destination);
        source2.connect(voiceGain).connect(destination);
        return destination.stream.getAudioTracks();
    }
    catch (err) {
    console.log(err);
    }
};

Firefox 目前不支持使用 getDisplayMedia 捕获音频。有一个feature request

您可以做的是在创建音频节点之前检查您的流是否有任何音轨,如下所示:

const destination = context.createMediaStreamDestination();

if (desktopStream.getAudioTracks().length) {
    const source1 = context.createMediaStreamSource(desktopStream);
    const desktopGain = context.createGain();
    desktopGain.gain.value = 0.7;
    source1.connect(desktopGain).connect(destination);
}

if (microPhoneStream.getAudioTracks().length) {
    const source2 = context.createMediaStreamSource(microPhoneStream);
    const voiceGain = context.createGain();
    voiceGain.gain.value = 0.7;
    source2.connect(voiceGain).connect(destination);
}

return destination.stream.getAudioTracks();