【发布时间】:2017-06-04 23:33:06
【问题描述】:
我正在尝试使用节点 js 和套接字 io 实时缓冲 MP3 歌曲。我基本上将 MP3 划分为字节段并将其发送到客户端,Web 音频 API 将在客户端接收它、解码并开始播放。这里的问题是声音不会连续播放,每个缓冲段之间有大约 0.5 秒的间隙。我该如何解决这个问题
// buffer is a 2 seconds decoded audio ready to be played
// the function is called when a new buffer is recieved
function stream(buffer)
{
// creates a new buffer source and connects it to the Audio context
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.loop = false;
// sets it and updates the time
source.start(time + context.currentTime);
time += buffer.duration; // time is global variable initially set to zero
}
调用stream的部分
// where stream bufferQ is an array of decoded MP3 data
// so the stream function is called after every 3 segments that are recieved
// the Web audio Api plays it with gaps between the sound
if(bufferQ.length == 3)
{
for(var i = 0, n = bufferQ.length ; i < n; i++)
{
stream(bufferQ.splice(0,1)[0]);
}
}
我应该使用网络音频 API 以外的其他 API,还是有办法安排我的缓冲区以便连续播放?
【问题讨论】:
-
何时调用
stream()? -
@guest271314 抱歉,我没有澄清这一点。我已经编辑了我的答案。你能看一下吗?
-
您可以在前一个
bufferSource的结尾处开始下一个bufferSource。您还可以利用MediaSource和updateend事件将媒体源片段的ArrayBuffer表示附加到媒体播放,请参阅HTML5 audio streaming: precisely measure latency? -
@guest271314 我试图通过执行类似 source.start(time + context.currentTime - 0.5);但是,它没有按预期工作。我将尝试使用 updateend。我是否应该创建一个媒体源,并在每次收到一个段时将新的解码缓冲区附加到它?感谢您的帮助和时间
-
非常感谢!我将阅读这个以及之前的堆栈溢出问题并尝试一下!
标签: javascript node.js socket.io buffer web-audio-api