如何在 Web 中播放来自 Android 的原始样本PCM_16音频数据记录(使用网络音频或其他)

How can i play raw samples PCM_16 audio data record from Android in Web (using Web-Audio or other)?

本文关键字:音频 记录 数据 其他 网络 PCM 播放 Web 样本 原始 Android      更新时间:2023-09-26

在Android上的应用程序中,我使用AudioRecord并连续发送字节数组PCM_16到Node.js服务器。

byte[] audioBuffer = new byte[mAudioBufferSampleSize];
    mAudioRecord.startRecording();
    int audioRecordingState = mAudioRecord.getRecordingState();
    if (audioRecordingState != AudioRecord.RECORDSTATE_RECORDING) {
        Log.e(TAG, "AudioRecord is not recording");
        return;
    } else {
        Log.v(TAG, "AudioRecord has started recording...");
    }
    while (inRecordMode) {
        int samplesRead = mAudioRecord.read(audioBuffer, 0,
                mAudioBufferSampleSize);
        Log.v(TAG, "Got samples: " + samplesRead);
        if (WebSocketManager.roomSocket.isConnected()) {            
            WebSocketManager.roomSocket.send(audioBuffer);
        }
    }

之后,我可以将其流式传输到 ArrayBuffer 类型的 Web 浏览器,并尝试将其转换为 Float32Array 作为 AudioContext 实例的缓冲区。但我听不到任何声音或大声喧哗。

    function onMessage(evt) {
    var context = new AudioContext();
    var source = context.createBufferSource();
    source.connect(context.destination);
    array = new Int8Array(evt.data);
    abs = new Float32Array(evt.data);
    arrays = new Float32Array(abs.length);
    ab = context.createBuffer(1, array.length, 44100);
    ab.getChannelData(0).set(arrays);
    source.buffer = ab;
    source.start(0);
        // then do it
    }

所以任何人都可以给我预付款,好吗?P/s:使用解码音频数据只是给出一个空错误

对不起,我的英语不好

你得到的数组是每个样本 0-32k(uint16 的范围)。 AudioBuffer 通道数据中的每个样本都是 float32 - 标称范围为 -1 到 +1。

您需要转换每个样本中的数据,而不仅仅是分配值并依赖转换。