嗨,我正在尝试使用 nodejs 在网络上制作广播流。我使用 ytdl npm 包获得了 youtube 视频音频的可读流
const ytdlStream = ytdl(videoLink,{filter:'audioonly',quality:'highestaudio'});
我想向所有监听器发送相同的数据包,所以当我收到一个 http 请求时,我将响应对象推送到一个数组
const server = http.createServer((req,res)=>{
console.log(req.url);
if(req.url === '/radyo'){
res.writeHead(200,{'Content-Type':'audio/mpeg','Connection':'keep-alive'});
writableStreams.push(res);
res.on('error',()=>{
writableStreams.splice(writableStreams.indexOf(res),1);
});
res.on('close',()=>{
writableStreams.splice(writableStreams.indexOf(res),1);
});
}
else if (req.url === '/'){
res.writeHead(200);
res.write('asdas famanas');
}
});
所以我有一组活动连接我使用这个数组发送每次新数据到达我的 ytdlStream
ytdlStream.on('readable',()=>{
let data;
while(data = ytdlStream.read()){
console.log('sent data ' + data.length + ' bytes');
for(writable of writableStreams){
writable.write(data);
}
}
});
我像每秒一样发送 16384 字节作为响应,但在浏览器上我只得到这个媒体资源
http://localhost/radyo could not be decoded, error: Error Code: NS_ERROR_DOM_MEDIA_METADATA_ERR (0x806e0006)
。可能这不是您将音频流式传输到 http 的方式。
https://uk3.internet-radio.com/proxy/majesticjukebox?mp=/live我想要这样的东西