连接"audio/x-wav"缓冲区并与"Transfer-Encoding"一起使用:"chunked" [英] Concatenating 'audio/x-wav' buffer and serving with 'Transfer-Encoding': 'chunked'

查看:243
本文介绍了连接"audio/x-wav"缓冲区并与"Transfer-Encoding"一起使用:"chunked"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是上一个问题的后续版本,由于@thejh而得以解决.

This question is a followup to a previous question, that was resolved thanks to @thejh.

我正在尝试将文本转换为音频并将数据作为分块"数据提供给客户端. 到目前为止,我的标题看起来像这样:

I'm trying to convert text to audio and serve the data to the client as 'chunked' data. So far my headers look like so:

res.writeHead(200, {
  'Content-Type': 'audio/wav',
   // I tried 'audio/x-wav' as well
  'Transfer-Encoding': 'chunked'
});

然后我将文本片段转换为队列中的音频(我以base64编码的数据形式获取),并像这样为它们提供服务:

And then I'm converting snippets of text to audio in a queue (which I get back as base64 encoded data), and serving them like so:

var src = Base64Audio.replace("data:audio/x-wav;base64,","");
var binAudio = new Buffer( src, 'base64');
res.write(binAudio);

所有信息都正在传输并发送到客户端,但是由于某种原因,浏览器(我正在Firefox 7.0.1中对其进行测试)正在播放它,并在第一个块之后停止.这是显示音频的过早结尾的结果/演示,这是

All the information is being transferred and sent to the client, but for some reason the browser (I'm testing it in Firefox 7.0.1) is playing it and stopping after the first chunk. Here's the result/demo showing the premature end of the audio, and this is the code being run on github.

这是为什么?这是因为x-wav数据具有诸如End的元数据或标头/元数据中的指定长度吗?如果是这样,此元数据是什么,是否有某种编辑方式,以使浏览器成功地将接收到的块连接在一起?

Why is this? Is this because the x-wav data has meta data like an End or specified length in is header/meta-data? If so what is this meta data and is there some way of editing it so that the browser successfully concatenates the received chunks?

我知道Header 'Content-Length': Buffer.length,但是在这种情况下,我不知道整个流将持续多长时间.

I'm aware of the Header 'Content-Length': Buffer.length but in this instance I don't know how long the entire stream is going to be.

任何人都可以提供建议.是否可以在发送缓冲区之前对其进行编辑,以使浏览器正确地将其连接起来?

Can anyone offer suggestions. Is it possible to Edit the buffers before they're sent such that the browser concatenates them properly?

推荐答案

我认为最好的选择是使它类似于音频流,就像icecast/shoutcast发送的一样.我认为FireFox在默认情况下不会处理此问题(旧版本则不会),这就是为什么有这么多可爱的Flash插件播放流的原因.

I think your best bet is to make this resemble an audio stream, as would be sent by icecast/shoutcast. I don't think FireFox will handle that by default (older versions do not), which is why there are so many lovely flash plugins to play streams.

您需要设置更多的标头以类似于流:

You need to set a few more headers to resemble a stream:

HTTP/1.0 200 OK
Content-Type: audio/wav
Transfer-Encoding: chunked
icy-br: ##
ice-audio-info:bitrate=##;samplerate=#####
icy-description:Some Name
icy-genre:Alternative
icy-name:Name
icy-pub:0
icy-url:http://yoursite.com
Server:Whatever you want
Cache-Control: no-cache
Connection: Keep-Alive

这篇关于连接"audio/x-wav"缓冲区并与"Transfer-Encoding"一起使用:"chunked"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆