简体   繁体   中英

node.js - share sync mp3 stream to several clients

I'm using binary.js ( http://binaryjs.com/ ) to stream mp3 file to several clients.

Server just starts to stream mp3 and what I want to achieve is that when client connects it starts to broadcast the stream not from the beginning but from the specific point of time in which playback is currently positioned. Some kind of synchronized playback for all clients.

Server:

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);
});

Client:

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);
  });
});

Is there a way to implement this?

You may wanna check this out question . It is very similar to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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