Below are some code snippets from three of my functions to start, pause, and resume a readable stream in Node.js. However, I would like a better way to control the Speaker() object besides initiating another one.
I am using the spotify-web module to get an audio stream from spotify. Could I just call new Speaker() each time instead of using a dedicated object? How can I address new Speaker() after the decoded stream has been piped to it?
The code below works for what I would like to do but I feel like there is a better way. I am new to Node.js and the idea of Passthrough Streams so any ideas or alternatives for stream control would be appreciated. Thanks in advance for any and all help!
// Lame decoder & speaker objects
var lame = new Lame.Decoder();
var spkr = new Speaker();
/* pipe a readable passthrough stream to the decoder
* and then to coreaudio via speaker obj.
*
* snippet from start stream function()
*/
stream
.pipe(lame)
.pipe(spkr)
/* unpipe the stream
* pause the stream at current position
*/
stream
.unpipe(lame)
.unpipe(spkr.end());
stream.pause();
/* stream from its last position
* how can I reuse spkr()?
*/
stream
.pipe(lame)
.pipe(new Speaker());
I ran into this same issue recently with the spotify-web module. The problem is that when you pipe it, the stream is no longer in flowing mode, so it can't be paused. One solution is to write each chunk of data to the decoder manually (essentially what piping would do automatically), as follows:
This way, you're free to call
stream.pause()andstream.resume()without worrying about piping and unpiping.If you're working with a spotify track and want to implement pause/play functionality, I would recommend using node-throttle to control the flow of the stream. Here's a simple example script:
Hope this was helpful. Here's a reference on streams in Node; it has some good information.