aftrumpet
aftrumpet

Reputation: 1289

How to create a real-time audio streaming queue?

So I'm thinking about creating a node application where users can add songs to a "queue" and have the songs be broadcasted to all users in real time, but after looking around I'm not quite sure how to accomplish this.

The primary article I read was this one: http://pedromtavares.wordpress.com/2012/12/28/streaming-audio-on-the-web-with-nodejs/

It seems like an icecast server could work very well for this, but is there a way for node to push songs to a queue to be played by the icecast server? So far from what I have read it seems the only way to manage songs played is to specify a playlist or add songs manually, and telling the server to not play anything when there are no songs in the queue also seems like a potential issue.

Upvotes: 4

Views: 2324

Answers (1)

Charles Crete
Charles Crete

Reputation: 964

I've been working on a similar project recently. My solution was to use nodeshout (node binding for libshout) to send audio data from Node to Icecast.

Check out the streaming example. You can use it like so:

function playSong(){
    // Choose next song
    const nextSong = "./song.mp3";
    const fileStream = new FileReadStream(nextSong, 65536);
    const shoutStream = fileStream.pipe(new ShoutStream(shout));

    shoutStream.on('finish',playSong);
}

playSong()

This will create a loop and play song after song.

Tip: Increase source timeout in your icecast.xml to ~30 seconds. In some cases, with the default, it causes the stream to end, due to songs having a "quick start", where the start of the song has a lower bitrate (to start playing faster).

I've made a Gist with a further example: https://gist.github.com/Cretezy/3623fecb1418e21b5d1f77db50fc7e07

Upvotes: 3

Related Questions