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:
// Lame decoder & speaker objects
var lame = new Lame.Decoder();
// pipe() returns destination stream
var spkr = lame.pipe(new Speaker());
// manually write data to the decoder stream,
// which is a writeable stream
stream.on('data', function (chunk) {
lame.write(chunk);
}
This way, you're free to call stream.pause()
and stream.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:
var Lame = require('lame');
var Speaker = require('speaker');
var Throttle = require('throttle');
var BIT_RATE = 160000; // Spotify web standard bit rate
// Lame decoder & speaker objects
var lame = new Lame.Decoder();
// pipe() returns destination stream
var spkr = lame.pipe(new Speaker());
// pipe the stream to a Throttle and
// set the stream as the Throttle
stream = stream.pipe(new Throttle(BIT_RATE/8)); // convert to bytes per second
// manually write data to the decoder stream,
// which is a writeable stream
stream.on('data', function (chunk) {
lame.write(chunk);
}
function pause() { stream.pause(); }
function resume() { stream.resume(); }
Hope this was helpful. Here's a reference on streams in Node; it has some good information.