diff --git a/lib/track.js b/lib/track.js index 1536df8..65f7f03 100644 --- a/lib/track.js +++ b/lib/track.js @@ -81,6 +81,18 @@ Track.prototype.play = function () { var spotify = this._spotify; var stream = new PassThrough(); + // Abort support + var req = null; + var isAborted = false; + stream.abort = function () { + if (isAborted === false) { + isAborted = true; + if (req !== null) { + req.abort(); + } + } + }; + // if a song was playing before this, the "track_end" command needs to be sent var track = spotify.currentTrack; if (track && track._playSession) { @@ -95,13 +107,15 @@ Track.prototype.play = function () { spotify.trackUri(track, function (err, res) { if (err) return stream.emit('error', err); if (!res.uri) return stream.emit('error', new Error('response contained no "uri"')); - debug('GET %s', res.uri); - track._playSession = res; - var req = spotify.agent.get(res.uri) - .set({ 'User-Agent': spotify.userAgent }) - .end() - .request(); - req.on('response', response); + if (isAborted === false) { + debug('GET %s', res.uri); + track._playSession = res; + req = spotify.agent.get(res.uri) + .set({ 'User-Agent': spotify.userAgent }) + .end() + .request(); + req.on('response', response); + } }); function response (res) {