javascript - 通过网络流式传输麦克风音频输入(RAW PCM)

标签 javascript html networking audio html5-audio

仅使用javascript录制原始pcm音频的最佳方法是什么?我将通过网络连接流式传输它,然后需要在另一侧重新组装数据包。网络部分已经完成,但是我似乎无法弄清楚如何录制,然后将原始的pcm数据包放入播放器中,然后让我在扬声器上听到。
谢谢你的帮助。

最佳答案

没有测试,但是这我会怎么做

  • 要求允许使用麦克风
  • 使用具有流的回调作为输入,并将其连接到ScriptProcessor
  • ScriptProcessor为您提供了一系列示例

  • 这里有一些代码片段可能会有所帮助
    // ask for permission
    navigator.getUserMedia( {audio:true}, successCB, errorCB );
    
    回调示例
    function successCB(stream) {
        const ctx = new AudioContext();
        const mediaStreamSource = ctx.createMediaStreamSource(stream);
    
        const scriptNode = ctx.createScriptProcessor(4096, 1, 1);
        scriptNode.onaudioprocess(audioProcessingEvent => {
            const inputBuffer = audioProcessingEvent.inputBuffer;
            const inputData = inputBuffer.getChannelData(0 /*channel*/);
    
            for (var sample = 0; sample < inputBuffer.length; sample++) {
                const THE_SAMPLE = inputData[sample];
                // save it in an array ?
            }
        });
        
        // connect the mic to the script
        mediaStreamSource.connect(scriptNode);
    
        // if you need to hear yourself (be careful with feedback or use headphones)
        mediaStreamSource.connect( ctx.destination );
    }
    
    仅供引用,对于我尚未尝试过的AudioWorkletNode,不推荐使用ScriptProcessor。

    关于javascript - 通过网络流式传输麦克风音频输入(RAW PCM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64033665/

    相关文章:

    javascript - ionic 日期选择器的最小值和最大值不适用于月份和日期

    javascript - 与 map 函数同步代码

    javascript - 我试图在 javascript 中获取数组中数字的平均值,但它没有按照我想要的方式工作

    html - 如何在 coldfusion 中检索 cookie?

    javascript - NuxtJS|在 header 组件中加载组件

    html - 如何跨帧显示叠加层?

    jquery - 使 <div>, <li> 无需 <a> 标签即可点击

    macos - Nmap 不检索 MAC 地址和供应商

    c - While 循环未正确迭代

    android - adb tcpip(ADB Over Wi-Fi)的安全性如何