nkzawa / socket.io-stream

Stream for Socket.IO
MIT License
610 stars 113 forks source link

Is it possible to Stream Audio form the Server to the client ? #73

Open daslicht opened 8 years ago

daslicht commented 8 years ago

Is it possible to Stream Audio form the Server to the client ? Is there any example please ?

duncanhoggan commented 8 years ago

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});
daslicht commented 8 years ago

AWESOME, i will try it soon ! <3 THANK YOU VERY MUCH!

daslicht commented 8 years ago

Is it also possible to send additional data with it like Metadata and is it even possible to send it to a specific point of time ?

duncanhoggan commented 8 years ago

You can emit it as a separate event or include it in the data object that is sent with the stream

ss(socket).on('audio-stream', function(stream, data) {}

daslicht commented 8 years ago

ok , but what about time sensitive data ? Lets say I like to push metadata once the Playback has reach 1Minute, 30 Seconds of the source ? Is at hat possible at all ?

NecroKills commented 7 years ago

Hi, Could you provide the full code?

zoutepopcorn commented 7 years ago

With this html part its working:

    <audio id="player" controls>
    <source src="" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>
zoutepopcorn commented 7 years ago

Example over here :D https://github.com/zoutepopcorn/audio_socket/tree/master

schnells commented 6 years ago

is there a way to do it in real time?

StefansArya commented 6 years ago

There is a way to stream microphone in a real time, but I have small problem on my implementation..

Update: I just uploaded this library, but it still need some improvement https://github.com/ScarletsFiction/SFMediaStream

avin3sh commented 5 years ago

@StefansArya Can I use that library to stream binary blobs (sent as chunks of 3-seconds) ? Finding it very difficult to implement

StefansArya commented 5 years ago

Hmm, I think you could only send the buffer data (without media header). Because that's what I can get after calling mediaRecorder.requestData().

To stream chunks of 3 seconds you need to change the latency to 3000ms. I have an example on the repository that default to 100ms, maybe you can use that for experimenting.

And don't forget to set the similar latency to the streamer too.

ithustle commented 5 years ago

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

@duncanhoggan , is it a good idea use sockets to build a streaming server to work as a streaming service on demand?

daslicht commented 5 years ago

that doesn't take bandwidth throttling into account no ? I just push as fast as possible no ?

yusuf987 commented 5 years ago

Yes it is possible Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

@duncanhoggan , is it a good idea use sockets to build a streaming server to work as a streaming service on demand?

ss is not defined

yusuf987 commented 5 years ago

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});

what is ss here ????

daslicht commented 5 years ago

what is ss here ????

see: https://github.com/nkzawa/socket.io-stream

rajatlnweb commented 5 years ago

The solution @duncanhoggan provided is working. But what it do is it firstly read the whole file then after that it plays. But What I want is, I want to stream the file into chunks. There can be the delay for max 4-5 seconds.

Can anyone please provide the solution for this?

avin3sh commented 5 years ago

@rajatlnweb if you send it in chunks of 4-5 seconds, it will play it that way too.

rajatlnweb commented 5 years ago

@avin3sh , Can you please give me the code example as I am not able to find it out anywhere?

zoutepopcorn commented 5 years ago

Look above: https://github.com/nkzawa/socket.io-stream/issues/73#issuecomment-307763328

rajatlnweb commented 5 years ago

@zoutepopcorn , I checked your code and it's not related to chunk based audio play at all.

jojomoore2007 commented 4 years ago

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});

Could you do that client -> server -> client? Like a peer-to-peer voice chat? Just trying to make something for my non-programmer friends.

duncanhoggan commented 4 years ago

@jojomoore2007 For that use case I would recommend using WebRTC or something similar, it would scale much better and would be a true peer to peer implementation as opposed to the server middleman.

viradiya1993 commented 3 years ago

Hello Streaming music synchronously from an mp3 file via an Angular + socket. io I have an mp3 file on my server.

And I want all my clients who visit that URL to listen to that music in sync.

That is.

Let's say the file plays for 6 minutes.

I start the song at 10:00 am

A request which comes at 10:03 am should start listening from the 3rd minute of the song.

All my clients should listen to the song in sync.

How can I achieve this with Angular and socket.io?

bailhongalGIT commented 2 years ago

Hello Streaming music synchronously from an mp3 file via an Angular + socket. io I have an mp3 file on my server.

And I want all my clients who visit that URL to listen to that music in sync.

That is.

Let's say the file plays for 6 minutes.

I start the song at 10:00 am

A request which comes at 10:03 am should start listening from the 3rd minute of the song.

All my clients should listen to the song in sync.

How can I achieve this with Angular and socket.io?

Hi @viradiya1993 , did you get any solution for this?

Jobin-S commented 1 year ago

isten to the song in sync.

I also need a solution for this. does anyone know how to implement this?