我正在使用 binary.js ( http://binaryjs.com/ ) 将 mp3 文件流式传输到多个客户端。
服务器刚刚开始传输 mp3,我想要实现的是,当客户端连接时,它不是从头开始广播流,而是从当前播放所在的特定时间点开始广播流。所有客户端的某种同步播放。
服务器:
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var server = require('http').createServer(handler);
server.listen(9000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var binaryserver = BinaryServer({server: server});
binaryserver.on('connection', function (client) {
var trackStream = fs.createReadStream(__dirname + '/longAudio.mp3');
client.send(trackStream);
});
客户:
var client = new BinaryClient('ws://localhost:9000');
client.on('stream', function (stream, meta) {
var parts = [];
stream.on('data', function(data){
parts.push(data);
});
stream.on('end', function () {
var audio = document.createElement('audio');
audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
audio.controls = 'controls';
document.body.appendChild(audio);
});
});
有办法实现吗?
最佳答案
您可能想看看 question 。 这和你的问题非常相似。
关于javascript - node.js - 向多个客户端共享同步 mp3 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758432/