Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emit success event after 200 response when playing track #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,28 @@ Track.prototype.metadata = function (fn) {
/**
* Begins playing this track, returns a Readable stream that outputs MP3 data.
*
* @param {Function} fn callback function
* @api public
*/

Track.prototype.play = function () {
Track.prototype.play = function (fn) {
// TODO: add formatting options once we figure that out
var spotify = this._spotify;
var stream = new PassThrough();
var returnValue = undefined;

// Maintain old compatibility
if ('function' !== typeof fn) {
var stream = new PassThrough();
fn = function (err, res) {
if (err) {
stream.emit('error', err);
} else {
res.pipe(stream);
}
};
// return stream immediately so it can be .pipe()'d
returnValue = stream;
}

// if a song was playing before this, the "track_end" command needs to be sent
var track = spotify.currentTrack;
Expand All @@ -93,8 +108,8 @@ Track.prototype.play = function () {

// initiate a "play session" for this Track
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"'));
if (err) return fn(err);
if (!res.uri) return fn(new Error('response contained no "uri"'));
debug('GET %s', res.uri);
track._playSession = res;
var req = spotify.agent.get(res.uri)
Expand All @@ -107,14 +122,13 @@ Track.prototype.play = function () {
function response (res) {
debug('HTTP/%s %s', res.httpVersion, res.statusCode);
if (res.statusCode == 200) {
res.pipe(stream);
fn(null, res);
} else {
stream.emit('error', new Error('HTTP Status Code ' + res.statusCode));
fn(new Error('HTTP Status Code ' + res.statusCode));
}
}

// return stream immediately so it can be .pipe()'d
return stream;
return returnValue;
};

/**
Expand Down