Skip to content
Tim Flapper edited this page May 4, 2014 · 33 revisions

Cordova Spotify Plugin API

Table of Contents

spotify API

spotify.Session API

spotify.AudioPlayer API

spotify.Playlist API

spotify.Album API

spotify.Artist API

spotify.Track API

spotify

Methods

spotify.authenticate

Authenticate the user with the Spotify API

spotify.authenticate(clientId, tokenExchangeURL, [scopes], callback)

clientId string

the clientId supplied by Spotify.

tokenExchangeURL string

The URL for your token exchange service. See setting up a token exchange service

scopes [optional] array

Custom scopes to request from the Spotify authentication service

callback function

The callback gets two arguments error and session. session is a spotify.Session object.

spotify.search

Search albums, artists or tracks. Return a maximum of 20 items. Use multiple requests with offset to receive more results.

spotify.search(query, searchType, [offset], session, callback)

query string

The query to search for.

searchType string

this can be one of three strings:

  • artist to perform an artist search.
  • album to perform an album search.
  • track to perform a track search.

offset [optional] integer

The starting index of the search results. If omitted the search will start from the first entry.

session spotify.Session

The spotify.Session object to use for authentication.

callback function

The callback gets two arguments error and result. result is an array of partial items. Each item has the following properties:

  • name. The name of the item.
  • uri. The URI of the item.

Use the uri with the correct spotify object to promote a partial item to a full item.

For example (Get the largest image for an artist from the search results):

var session = ...a valid spotify.Session object...
var item = ...a single artist search item.....

spotify.Artist(item.uri, session, function(error, artist) {
	var image = artist.largestImage;
	
	console.log('Image URL: %s', image.imageURL);
	console.log('Image aspect ratio: %f', image.aspect);
	console.log('Image size: %s', image.aspect);
});

spotify.getPlaylistsForUser

Get an array of playlists for user with username.

spotify.getPlaylistsForUser(username, session, callback);

username string

The username of the user to request the playlists from.

session spotify.Session

The spotify.Session object to use for authentication.

callback function

The callback gets two arguments error and result. result is an array of partial items, see spotify.search for an explanation of partial items.

spotify.Session

Normally a spotify.Session object is returned from spotify.authenticate() but you can use the spotify.Session object to store the session for later use. A session is valid for 24 hours after which the user will need to login again.

var session = spotify.Session({username: 'someUsername', credentials: 'AFD42....GD43'});

Properties

session.username

The username of the user.

session.credential

An access token to verify the session.

spotify.AudioPlayer

The constructor for a new spotify.AudioPlayer object.

spotify.AudioPlayer(companyName, appName, session, callback)

companyName string

Your company name

appName string

Your application name

session object

The user's spotify.Session object.

callback function

The callback gets two arguments error and an spotify.AudioPlayer object, ready to start playing tracks.

Example

These examples show how to construct a spotify.AudioPlayer and play a track.

spotify.AudioPlayer('Your-Company-Name', 'Your-App-Name', session, function(error, audioPlayer) {
	audioPlayer.playURI('spotify:track:6JEK0CvvjDjjMUBFoXShNZ');
});

Methods

audioPlayer.addEventListener

Add an event listener to the audio player.

audioPlayer.addEventListener(event, listener)

audioPlayer.playURI

Play the audio track belonging to the supplied Spotify track URI.

audioPlayer.playURI(uri, callback)

uri string

The URI belonging to the track to play.

callback function

The callback gets one argument error. The error argument will be null if successful or an Error object if something went wrong.

audioPlayer.seekToOffset

Skip the track to the specified offset.

audioPlayer.seekToOffset(offset, callback)

offset Number

The new playback position (offset) in seconds.

callback function

The callback gets one argument error. The error argument will be null if successful or an Error object if something went wrong.

audioPlayer.getIsPlaying

Request the current playback status.

audioPlayer.getIsPlaying(callback)

callback function

The callback gets two arguments error and status. The status value will be true if playing or false if paused / stopped.

audioPlayer.setIsPlaying

Set the current playback status. This can be seen as a method for pausing / resuming playback.

audioPlayer.setIsPlaying(status, callback)

status boolean

The new playback status.

callback function

The callback gets one argument error. The error argument will be null if successful or an Error object if something went wrong.

audioPlayer.getVolume

Request the playback volume.

audioPlayer.getVolume(callback)

callback function

The callback gets two arguments error and volume. The volume value will be a numeric value between 0.0 and 1.0.

audioPlayer.setVolume

Set the playback volume.

audioPlayer.setVolume(volume, callback)

volume Number

The new playback volume between 0.0 and 1.0.

callback function

The callback gets one argument error. The error argument will be null if successful or an Error object if something went wrong.

audioPlayer.getLoggedIn

Request the current login status of the audio player.

audioPlayer.getLoggedIn(callback)

callback function

The callback gets one argument error. The error argument will be null if successful or an Error object if something went wrong.

audioPlayer.getCurrentTrack

Request information about the track that is currently playing.

audioPlayer.getCurrentTrack(callback)

callback function

The callback gets two arguments error and track. The track value will be an object literal with the track's metadata.

audioPlayer.getCurrentPlaybackPosition

Request the current playback position.

audioPlayer.getCurrentPlaybackPosition(callback)

callback function

The callback gets two arguments error and offset. The offset value will be a numeric value in seconds between 0.0 and the duration of the track.

Events

login

This event is dispatched when the player has logged in.

logout

This event is dispatched when the player is logged out.

permissionLost

This event is dispatched when the playback permission is lost. This happens mostly when the user plays a track on another device.

error

This event is dispatched whenever an error occurs in the audio player.

The listener method receives a single string argument error.

message

This event is dispatched when a message is sent by Spotify to the user. This message has to be shown to the user see the iOS SDK reference.

The listener method receives a single string argument message.

playbackStatus

This event is dispatched when the playback status has changed.

The listener method receives a single boolean argument status.

seekToOffset

This event is dispatched if the playback offset has changed "unnaturally".

The listener method receives a single Number argument offset.

spotify.Playlist

Methods

playlist.setName

playlist.setDescription

playlist.setCollaborative

playlist.addTracks

playlist.delete

Properties

playlist.name

playlist.version

playlist.uri

playlist.collaborative

playlist.creator

playlist.tracks

playlist.dateModified

spotify.Album

Properties

album.name

album.uri

album.sharingURL

album.externalIds

album.availableTerritories

album.artists

album.tracks

album.releaseDate

album.type

album.genres

album.covers

album.largestCover

album.smallestCover

album.popularity

spotify.Artist

Properties

artist.name

artist.uri

artist.sharingURL

artist.genres

artist.images

artist.smallestImage

artist.largestImage

artist.popularity

spotify.Track

Properties

track.name

track.uri

track.sharingURL

track.previewURL

track.duration

track.artists

track.album

track.trackNumber

track.discNumber

track.popularity

track.flaggedExplicit

track.externalIds

track.availableTerritories

Clone this wiki locally