简体   繁体   中英

Spotify Apps - Getting Playlist Tracks

I'm developing a Spotify app and want to get the tracks for a Playlist. There's the function playlist.tracks but that seems to be cached and gets the wrong list of tracks. It's also supposed to be slow and not recommended to be used in the API documentation. But what other option do I have to get a Playlist's tracks? At the moment I'm using playlist.tracks once and informing my back-end of the track list.

Thanks.

For the 1.0 API:

require([
  '$api/models'
], function (models) { 

 var addList = function(list) {
    list.load('tracks').done(function(list) {
        list.tracks.snapshot().done(function(trackSnapshot){
            var tracks = trackSnapshot.toArray();
            for(var i=0; i<tracks.length; i++) {
                addTrack(tracks[i]);
            }
        });
    });
 }

 var addTrack = function(track) {
    track.load('name','artists').done(function(track) {
        var artists = track.artists;
        var artistsList = [];
        for (var i=0; i<artists.length; i++) {
            artistsList.push(artists[i].name);
        }
        // Print out info if desired
        var addedTrackRow = document.createElement('p');
        addedTrackRow.innerHTML = "Added track: " + artistsList.join(', ') + ' - ' + track.name;
        document.getElementById('addedTracks').appendChild(addedTrackRow);

    });
 }

 exports.addList = addList;
 exports.addTrack = addTrack;
}

// Example of use:
addList(models.Playlist.fromURI(...))
addTrack(models.Track.fromURI(...))

I've tested it as used above, and it works.

I spent some time looking for an answer to this myself, and I also were looking for how to access tracks within a list even though it's explained in the tutorial-app available on github under the "Metadata"-section; "Get metadata from an artist, album, track or playlist".

I hope this is usefull.

Here is a small sample :

pl = new m.Playlist();
alb = m.Album.fromURI(uri, function(album) { 
   pl.name = album.name;
   $.each(album.tracks,function(index,track){
      pl.add(m.Track.fromURI(track.uri));   
   });

Hope it could help....

$.each(pl.data.all(),function(i,track){ myAwesomePlaylist.add(track); }); 

where pl is the playlist

The class Playlist implements the interface Collection . So you can invoke the function get over playlist object in a loop to get all the tracks. Example code:

var i=0;
for (i=0;i<models.library.starredPlaylist.length;i++)
{
    var track = models.library.starredPlaylist.get(i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM