簡體   English   中英

使用JavaScript播放原始音頻

[英]Play raw audio with JavaScript

我有這樣的數字流

-0.00015259254737998596,-0.00009155552842799158,0.00009155552842799158,0.00021362956633198035,0.0003662221137119663,0.0003967406231879635,0.00024414807580797754,0.00012207403790398877,0.00012207403790398877,0.00012207403790398877,0.0003357036042359691,0.0003357036042359691,0.00018311105685598315,0.00003051850947599719,0,-0.00012207403790398877,0.00006103701895199438,0.00027466658528397473,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003662221137119663,0.0004882961516159551,0.0004577776421399579,0.00027466658528397473,0.00003051850947599719,-0.00027466658528397473....

據說代表音頻流。 我從這里獲取它們,並已通過網絡傳輸它們,現在我正在嘗試播放實際的聲音,並且從此處獲取了一個片段,但我卻Uncaught (in promise) DOMException: Unable to decode audio data

我覺得我想念很多東西,只是希望它能像魔術一樣工作,事實並非如此。

我的密碼

var ws = new WebSocket("ws://....");
ws.onmessage = function (event) {
  playByteArray(event.data);
}

var context = new AudioContext();
function playByteArray(byteArray) {

  var arrayBuffer = new ArrayBuffer(byteArray.length);
  var bufferView = new Uint8Array(arrayBuffer);
  for (var i = 0; i < byteArray.length; i++) {
    bufferView[i] = byteArray[i];
  }

  context.decodeAudioData(arrayBuffer, function (buffer) {
    buf = buffer;
    play();
  });
}

// Play the loaded file
function play() {
  // Create a source node from the buffer
  var source = context.createBufferSource();
  source.buffer = buf;
  // Connect to the final output node (the speakers)
  source.connect(context.destination);
  // Play immediately
  source.start(0);
}

和廣播部分

var ws = new WebSocket("ws://.....");
window.addEventListener("audioinput", function onAudioInput(evt) {
  if (ws) {
    ws.send(evt.data);
  }
}, false);

audioinput.start({
    bufferSize: 8192
});

看起來您正在處理兼容的音頻數據格式。 鏈接到的代碼用於播放字節數組,在這種情況下,您的音頻數據應該是(更長)整數字符串,范圍是0到255。

您所擁有的是相當短的(隨着音頻數據的流逝)一串浮點數。 我無法確定應該使用哪種音頻格式,但是需要使用其他播放器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM