【问题标题】:Stream Web Audio with WebRTC without asking for microphone使用 WebRTC 流式传输 Web 音频,无需麦克风
【发布时间】:2021-05-12 18:37:20
【问题描述】:

我想使用 WebRTC 将音频从网页流式传输到本地服务器。该服务器将处理该音频并将其立即输出给用户。我需要实时。

我的代码实际上正在运行。但是我用 getUserMedia 向用户询问麦克风,我不需要那个麦克风。这很烦人。我可以做些什么来流式传输音频而无需向用户询问麦克风?

谢谢。

这是一个最小的工作示例(它深受https://github.com/aiortc/aiortc/blob/main/examples/server/client.js 的启发)。只有 cmets 的最后一部分很有趣:

let webSocket = new WebSocket('wss://0.0.0.0:8080/ws');
const config = { sdpSemantics: 'unified-plan' }

const pc = new RTCPeerConnection(config);

webSocket.onmessage = (message) => {
    const data = JSON.parse(message.data); 
    switch(data.type) {
        case "answer":
            pc.setRemoteDescription(data.answer)
            break;
        default: 
            break; 
    }
};

function negotiate() {
    return pc.createOffer()
    .then(function(offer) {
        return pc.setLocalDescription(offer);
    })
    .then(function() {
        return new Promise(function(resolve) {
            if (pc.iceGatheringState === 'complete') {
                resolve();
            } else {
                function checkState() {
                    if (pc.iceGatheringState === 'complete') {
                        pc.removeEventListener('icegatheringstatechange', checkState);
                        resolve();
                    }
                }
                pc.addEventListener('icegatheringstatechange', checkState);
            }
        });
    })
    .then(function() {
        const offer = pc.localDescription;
        webSocket.send(
            JSON.stringify({
                type: "offer",
                offer: {
                    sdp: offer.sdp,
                    type: offer.type
                }
            })
        );
    })
}

// Preparing the oscillator
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioCtx.createOscillator();
const serverDestination = audioCtx.createMediaStreamDestination();
oscillator.connect(serverDestination);

// Asking for useless microphone
navigator.mediaDevices.getUserMedia({audio: true})
.then(() => {
    return negotiate();
});

// Actual streaming
const stream = new MediaStream();
serverDestination.stream.getTracks().forEach((track) => {
    pc.addTrack(track, stream);
})

// User pushes button to start the oscillator
function play() {
    oscillator.start();
};

【问题讨论】:

标签: webrtc audio-streaming web-audio-api


【解决方案1】:

摆脱这个:

// Asking for useless microphone
navigator.mediaDevices.getUserMedia({audio: true})
.then(() => {
    return negotiate();
});

正如你所说,它没用也没有必要。如果您不拨打getUserMedia(),则不会提示用户共享他们的麦克风。你可以在没有这个的情况下建立 WebRTC 连接。

我怀疑您遇到的问题是您的音频上下文已暂停。如果您在用户单击按钮时调用audioCtx.resume(),您将启动并运行。这是由于自动播放政策。

【讨论】:

  • 感谢您的回答。我忘了在我的“最小示例”中考虑自动播放策略。在我的真实代码中,用户必须按下一个按钮。我编辑了我的例子。如果我用简单的“协商”替换整个“getUserMedia”并添加 audioCtx.resume(),它仍然不起作用。
【解决方案2】:

如果您不需要用户媒体,请不要在代码中使用 getUserMedia 来要求它。

【讨论】:

    猜你喜欢
    • 2014-12-20
    • 2018-07-29
    • 2014-10-23
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多