nativescript-spotify
Version:
Spotify for your NativeScript app.
500 lines • 18.9 kB
JavaScript
var observable_1 = require('data/observable');
var common_1 = require('../common');
var auth_1 = require('./auth');
var TNSSpotifyPlayer = (function (_super) {
__extends(TNSSpotifyPlayer, _super);
function TNSSpotifyPlayer() {
_super.apply(this, arguments);
this._started = false;
this._loggedIn = false;
this._playerLoggedIn = false;
}
TNSSpotifyPlayer.prototype.initPlayer = function (emitEvents) {
var _this = this;
this.auth = new auth_1.TNSSpotifyAuth();
if (emitEvents) {
this.auth.setupEvents();
this.setupEvents();
}
auth_1.TNSSpotifyAuth.VERIFY_SESSION().then(function () {
_this.setLoggedIn(true);
_this.playerReady();
}, function () {
_this.setLoggedIn(false);
_this.playerReady();
});
};
TNSSpotifyPlayer.prototype.isLoggedIn = function () {
return this._loggedIn;
};
TNSSpotifyPlayer.prototype.togglePlay = function (track, force) {
var _this = this;
return new Promise(function (resolve, reject) {
if (track && (track !== _this._loadedTrack)) {
_this.play(track).then(resolve, reject);
}
else if (_this.player) {
var playState = typeof force !== 'undefined' ? force : !_this.player.isPlaying;
_this.player.setIsPlayingCallback(playState, function (error) {
if (error != null) {
console.log("*** Pause/Resume playback got error:");
console.log(error);
auth_1.TNSSpotifyAuth.VERIFY_SESSION(auth_1.TNSSpotifyAuth.SESSION).then(function () {
var origResolve = resolve;
_this.togglePlay(track, force).then(function (isPlaying) {
origResolve(isPlaying);
});
}, function () {
if (_this.isLoginError(error.localizedDescription)) {
_this.loginError();
reject('login');
}
else {
reject(false);
}
});
return;
}
resolve(_this.player.isPlaying);
});
}
});
};
TNSSpotifyPlayer.prototype.isPlaying = function () {
if (this.player) {
return this.player.isPlaying;
}
else {
return false;
}
};
TNSSpotifyPlayer.prototype.loadedTrack = function () {
return this._loadedTrack;
};
TNSSpotifyPlayer.prototype.setVolume = function (val) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.player) {
_this.player.setVolumeCallback(val, function (error) {
if (error !== null) {
console.log("Spotify Player volume adjust error:", error);
reject(error);
return;
}
resolve();
});
}
else {
reject();
}
});
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangePlaybackStatus = function (controller, playing) {
console.log("DidChangePlaybackStatus: " + playing);
if (this.events) {
this._changedPlaybackStatus.data.playing = playing;
this.events.notify(this._changedPlaybackStatus);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangePlaybackState = function (controller, state) {
console.log("DidChangePlaybackState: " + state);
if (this.events) {
this._changedPlaybackState.data.state = state;
this.events.notify(this._changedPlaybackState);
this.updateCoverArt(state.currentTrack.albumUri);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidSeekToOffset = function (controller, offset) {
console.log("DidSeekToOffset: " + offset);
if (this.events) {
this._seekedToOffset.data.offset = offset;
this.events.notify(this._seekedToOffset);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangeVolume = function (controller, volume) {
console.log("DidChangeVolume: " + volume);
if (this.events) {
this._changedVolume.data.volume = volume;
this.events.notify(this._changedVolume);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangeShuffleStatus = function (controller, isShuffled) {
console.log("DidChangeShuffleStatus: " + isShuffled);
if (this.events) {
this._changedShuffleStatus.data.shuffle = isShuffled;
this.events.notify(this._changedShuffleStatus);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangeRepeatStatus = function (controller, isRepeated) {
console.log("DidChangeRepeatStatus: " + isRepeated);
if (this.events) {
this._changedRepeatStatus.data.repeat = isRepeated;
this.events.notify(this._changedRepeatStatus);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidChangeToTrack = function (controller, trackMetadata) {
console.log("DidChangeToTrack: " + trackMetadata);
if (this.events) {
this._changedToTrack.data.metadata = trackMetadata;
this.events.notify(this._changedToTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidFailToPlayTrack = function (controller, trackUri) {
console.log("DidFailToPlayTrack: " + trackUri.absoluteString);
if (this.events) {
this._failedToPlayTrack.data.url = trackUri.absoluteString;
this.events.notify(this._failedToPlayTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidStartPlayingTrack = function (controller, trackUri) {
console.log("DidStartPlayingTrack: " + trackUri.absoluteString);
this.updateCoverArt(this.currentTrackMetadata().albumUri);
if (this.events) {
this._startedPlayingTrack.data.url = trackUri.absoluteString;
this.events.notify(this._startedPlayingTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidStopPlayingTrack = function (controller, trackUri) {
console.log("DidStopPlayingTrack: " + trackUri.absoluteString);
if (this.events) {
this._stoppedPlayingTrack.data.url = trackUri.absoluteString;
this.events.notify(this._stoppedPlayingTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidSkipToNextTrack = function (controller) {
console.log("DidSkipToNextTrack");
if (this.events) {
this.events.notify(this._skippedToNextTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidSkipToPreviousTrack = function (controller) {
console.log("DidSkipToPreviousTrack");
if (this.events) {
this.events.notify(this._skippedToPreviousTrack);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidBecomeActivePlaybackDevice = function (controller) {
console.log("DidBecomeActivePlaybackDevice");
if (this.events) {
this.events.notify(this._activePlaybackDevice);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidBecomeInactivePlaybackDevice = function (controller) {
console.log("DidBecomeInactivePlaybackDevice");
if (this.events) {
this.events.notify(this._inactivePlaybackDevice);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidPopQueue = function (controller) {
console.log("DidPopQueue");
if (this.events) {
this.events.notify(this._poppedQueue);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidEncounterTemporaryConnectionError = function (controller) {
console.log("audioStreamingDidEncounterTemporaryConnectionError");
if (this.events) {
this.events.notify(this._temporaryConnectionError);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidEncounterError = function (controller, error) {
console.log("audioStreamingDidEncounterTemporaryConnectionError");
console.log(error);
if (this.events) {
this._streamError.data.error = error;
this.events.notify(this._streamError);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidReceiveMessage = function (controller, message) {
console.log("audioStreamingDidReceiveMessage");
if (this.events) {
this._receivedMessage.data.message = message;
this.events.notify(this._receivedMessage);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidDisconnect = function (controller) {
console.log("audioStreamingDidDisconnect");
if (this.events) {
this.events.notify(this._streamDisconnected);
}
};
TNSSpotifyPlayer.prototype.audioStreamingDidLogin = function () {
var _this = this;
console.log("audioStreamingDidLogin");
this._playerLoggedIn = true;
var handlePromise = function (success) {
if (_this._loggingInPromise) {
if (success) {
_this._loggingInPromise.resolve();
}
else {
_this._loggingInPromise.reject();
}
_this._loggingInPromise = undefined;
}
};
auth_1.TNSSpotifyAuth.CHECK_PREMIUM().then(function () {
handlePromise(true);
}, function () {
handlePromise(false);
});
};
TNSSpotifyPlayer.prototype.audioStreamingDidLogout = function (controller) {
console.log("audioStreamingDidLogout");
var errorRef = new interop.Reference();
if (!this.player.stopWithError(errorRef)) {
if (errorRef) {
console.log("stopWithError:");
for (var key in errorRef) {
console.log(errorRef[key]);
}
}
}
this.player = undefined;
auth_1.TNSSpotifyAuth.LOGOUT();
};
TNSSpotifyPlayer.prototype.play = function (track) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.checkPlayer().then(function () {
_this.playUri(track, resolve, reject);
}, function () {
reject('login');
});
});
};
TNSSpotifyPlayer.prototype.playUri = function (track, resolve, reject) {
var _this = this;
console.log(track);
this.player.playURIStartingWithIndexCallback(NSURL.URLWithString(track), 0, function (error) {
if (error != null) {
console.log("*** playURICallback got error:");
console.log(error);
if (_this.isLoginError(error.localizedDescription)) {
_this.loginError();
reject('login');
}
else {
reject(false);
}
return;
}
_this._loadedTrack = track;
resolve(true);
});
};
TNSSpotifyPlayer.prototype.checkPlayer = function () {
var _this = this;
var printErrorRef = function (ref, failure) {
console.log("SPTAudioStreamingController.sharedInstance().startWithClientIdError Start" + (failure ? ' Failure' : '') + ":", ref);
for (var key in ref) {
console.log(ref[key]);
}
};
return new Promise(function (resolve, reject) {
if (!_this._started) {
var errorRef = new interop.Reference();
_this.player = SPTAudioStreamingController.sharedInstance();
if (_this.player.startWithClientIdAudioControllerAllowCachingError(common_1.TNSSpotifyConstants.CLIENT_ID, null, false, errorRef)) {
_this._started = true;
_this.player.delegate = _this;
_this.player.playbackDelegate = _this;
if (errorRef) {
console.log(errorRef.description);
printErrorRef(errorRef, false);
}
if (!_this._playerLoggedIn) {
_this._loggingInPromise = {
resolve: resolve,
reject: reject
};
_this.player.loginWithAccessToken(auth_1.TNSSpotifyAuth.SESSION.accessToken);
}
else {
auth_1.TNSSpotifyAuth.CHECK_PREMIUM().then(resolve, reject);
}
}
else {
_this._started = false;
if (errorRef) {
printErrorRef(errorRef, true);
}
auth_1.TNSSpotifyAuth.CHECK_PREMIUM().then(reject, reject);
}
}
else {
resolve();
}
});
};
TNSSpotifyPlayer.prototype.updateCoverArt = function (albumUri) {
var _this = this;
return new Promise(function (resolve, reject) {
SPTAlbum.albumWithURISessionCallback(NSURL.URLWithString(albumUri), auth_1.TNSSpotifyAuth.SESSION, function (error, albumObj) {
if (error != null) {
console.log("*** albumWithURISessionCallback got error:");
console.log(error);
reject();
return;
}
_this._currentAlbumImageUrl = albumObj.largestCover.imageURL.absoluteString;
if (_this.events) {
_this._albumArtChange.data.url = _this._currentAlbumImageUrl;
_this.events.notify(_this._albumArtChange);
}
resolve();
});
});
};
TNSSpotifyPlayer.prototype.isLoginError = function (desc) {
if (desc.indexOf('invalid credentials') > -1 || desc.indexOf('NULL') > -1) {
return true;
}
else {
return false;
}
};
TNSSpotifyPlayer.prototype.loginError = function () {
this.setLoggedIn(false);
common_1.Utils.alert('You need to login to renew your session.');
};
TNSSpotifyPlayer.prototype.setLoggedIn = function (value) {
var _this = this;
this._loggedIn = value;
if (!value) {
this._playerLoggedIn = false;
if (this._started) {
this._started = false;
console.log("streamingcontroller logout()");
this.player.logout();
setTimeout(function () {
if (_this.player) {
_this.audioStreamingDidLogout(null);
}
}, 1000);
}
}
};
TNSSpotifyPlayer.prototype.playerReady = function () {
if (this.events) {
this._playerReady.data.loggedIn = this._loggedIn;
this.events.notify(this._playerReady);
}
};
TNSSpotifyPlayer.prototype.setupEvents = function () {
var _this = this;
this.auth.events.on('authLoginChange', function (eventData) {
_this.setLoggedIn(eventData.data.status);
});
this.events = new observable_1.Observable();
this._albumArtChange = {
eventName: 'albumArtChange',
data: {
url: ''
}
};
this._playerReady = {
eventName: 'playerReady',
data: {
loggedIn: false
}
};
this._changedPlaybackStatus = {
eventName: 'changedPlaybackStatus',
data: {
playing: false
}
};
this._changedPlaybackState = {
eventName: 'changedPlaybackState',
data: {
state: {}
}
};
this._seekedToOffset = {
eventName: 'seekedToOffset',
data: {
offset: 0
}
};
this._changedVolume = {
eventName: 'changedVolume',
data: {
volume: 0
}
};
this._changedShuffleStatus = {
eventName: 'changedShuffleStatus',
data: {
shuffle: false
}
};
this._changedRepeatStatus = {
eventName: 'changedRepeatStatus',
data: {
repeat: false
}
};
this._changedToTrack = {
eventName: 'changedToTrack',
data: {
metadata: null
}
};
this._failedToPlayTrack = {
eventName: 'failedToPlayTrack',
data: {
url: null
}
};
this._startedPlayingTrack = {
eventName: 'startedPlayingTrack',
data: {
url: null
}
};
this._stoppedPlayingTrack = {
eventName: 'stoppedPlayingTrack',
data: {
url: null
}
};
this._skippedToNextTrack = {
eventName: 'skippedToNextTrack'
};
this._skippedToPreviousTrack = {
eventName: 'skippedToPreviousTrack'
};
this._activePlaybackDevice = {
eventName: 'activePlaybackDevice'
};
this._inactivePlaybackDevice = {
eventName: 'inactivePlaybackDevice'
};
this._poppedQueue = {
eventName: 'poppedQueue'
};
this._temporaryConnectionError = {
eventName: 'temporaryConnectionError'
};
this._streamError = {
eventName: 'streamError',
data: {
error: null
}
};
this._receivedMessage = {
eventName: 'receivedMessage',
data: {
message: null
}
};
this._streamDisconnected = {
eventName: 'streamDisconnected'
};
};
TNSSpotifyPlayer.ObjCProtocols = [SPTAudioStreamingDelegate, SPTAudioStreamingPlaybackDelegate];
return TNSSpotifyPlayer;
}(NSObject));
exports.TNSSpotifyPlayer = TNSSpotifyPlayer;
//# sourceMappingURL=player.js.map