简体   繁体   English

Spotify应用-获取播放列表曲目

[英]Spotify Apps - Getting Playlist Tracks

I'm developing a Spotify app and want to get the tracks for a Playlist. 我正在开发Spotify应用,并希望获取播放列表的曲目。 There's the function playlist.tracks but that seems to be cached and gets the wrong list of tracks. 有功能playlist.tracks但似乎已缓存并获得了错误的曲目列表。 It's also supposed to be slow and not recommended to be used in the API documentation. 它也应该很慢,不建议在API文档中使用。 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. 此刻,我只使用一次playlist.tracks并将后端playlist.tracks通知我的后端。

Thanks. 谢谢。

For the 1.0 API: 对于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; 我花了一些时间自己寻找答案,而且我也在寻找如何访问列表中的曲目,即使它在github上“ Metadata”部分的“ tutorial-app”中已经说明了; "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 其中pl是播放列表

The class Playlist implements the interface Collection . Playlist类实现了Collection接口。 So you can invoke the function get over playlist object in a loop to get all the tracks. 因此,您可以在循环中调用函数get over playlist对象来获取所有曲目。 Example code: 示例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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