Spotify Apps - Getting Playlist Tracks

穿精又带淫゛_ 提交于 2019-12-08 12:17:58

问题


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.


回答1:


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.




回答2:


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




回答3:


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);
}


来源:https://stackoverflow.com/questions/9362454/spotify-apps-getting-playlist-tracks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!