简体   繁体   English

HTML5中的音频数据流

[英]Audio data streaming in HTML5

I am receiving PCM audio data from the server in small chunks and having them stored in an Array. 我正在以小块的形式从服务器接收PCM音频数据,并将它们存储在一个数组中。 Now I would like to play these audio chunks sequentially without gaps using some HTML5 capability. 现在我想使用一些HTML5功能顺序播放这些音频块。 Two options which I am looking at as 'possible' solutions are: 我将其视为“可能”解决方案的两个选项是:

  1. HTML5 Audio tag with Data URI 带有数据URI的 HTML5音频标记
  2. Web audio API Web音频API

While I am investigating these options, please suggest me any other option or views on the two options I am looking at. 在我调查这些选项时,请向我建议我正在考虑的两个选项的任何其他选项或观点。 Though a cross platform solution will be the best but I can settle for Chrome only solution as 虽然跨平台解决方案将是最好的,但我可以满足Chrome专用解决方案

Scheduling audio is something the Web Audio API was designed for. 调度音频是Web Audio API的设计目标。 If you have the decoded PCM audio chunks as typed arrays ( AUDIO_CHUNKS ), you can create audio buffers for each chunk, and schedule them at an exact time (one after the other) using noteOn() . 如果将已解码的PCM音频块作为类型化阵列( AUDIO_CHUNKS ),则可以为每个块创建音频缓冲区,并使用noteOn()在准确的时间(一个接一个)安排它们。 Something like: 就像是:

var startTime = 0;

for (var i = 0, audioChunk; audioChunk = AUDIO_CHUNKS[i]; ++i) {
  // Create/set audio buffer for each chunk
  var audioBuffer = audioCtx.createBuffer(NUM_CHANNELS, NUM_SAMPLES, SAMPLE_RATE);
  audioBuffer.getChannelData(0).set(audioChunk);

  var source = audioCtx.createBufferSource();
  source.buffer = audioBuffer;
  source.noteOn(startTime);
  source.connect(audioCtx.destination);

  startTime += audioBuffer.duration;
}

Option 1 will probably not work because the audio tag doesn't play raw audio data (which I assume is what you mean by PCM audio data, or am I wrong?). 选项1可能不起作用,因为音频标签不播放原始音频数据(我假设你是PCM音频数据的意思,或者我错了?)。 Every browser needs specific codecs. 每个浏览器都需要特定的编解码 To top it of the audio tag is not reliable at all to play things without gaps. 最重要的是音频标签在没有间隙的情况下完全不可靠。

Option 2 might work. 选项2可能有效。 The web audio api contains buffers which probably could be filled with raw data and played, but I've never tried doing so. 网络音频api包含可能填充原始数据并播放的缓冲区,但我从未尝试过这样做。 The big drawback right now is that; 现在最大的缺点是; a. 一个。 Chrome only b. Chrome仅限b。 the user needs to configure chrome by typing about:flags and enable Web Audio which might be scary to some. 用户需要通过键入about:flags并启用Web Audio来配置chrome,这可能对某些人来说可怕。

A third option would be the Audio Data API which is something of a middle ground. 第三种选择是音频数据API,这是一个中间立场。 I've never tried it myself, but from the spec it looks like exactly what you're looking for. 我自己从未尝试过,但从规格来看,它看起来就像你正在寻找的那样。 I dunno about implementations though, so you'd have to do some research on your own :) https://wiki.mozilla.org/Audio_Data_API#Writing_Audio 我不赞成实现,所以你必须自己做一些研究:) https://wiki.mozilla.org/Audio_Data_API#Writing_Audio

Please not that I'm giving these answers on top of my head, and I'm still pretty new to HTML5 audio. 请注意,我正在给出这些答案,而且我仍然是HTML5音频的新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM