Spotify创建播放列表并添加曲目

时间:2014-01-09 17:54:29

标签: javascript api spotify spotify-app

我正在尝试在我的应用API 1.0中添加“另存为播放列表”按钮。

有人可以帮我弄清楚这里有什么问题吗?

var player_obj = models.Album.fromURI('spotify:album:7o7UHh5PfO1kY4YoxqrwN7');
player_obj.load('tracks').done(function(a){
    var saved_playlist = new models.Playlist.create(player_obj.name);
    console.log(saved_playlist);
    a.tracks.snapshot().done(function(snapshot){
        for (var i = 0, l = snapshot.length; i < l; i++) {
            var track = snapshot.get(i);
            saved_playlist.add(track);
        }
    });
});

获取错误: 未捕获的TypeError:Object#没有方法'add'

播放列表已创建,但它是空的。

当player_obj是一个播放列表时,我得到了同样的错误,但是播放列表部分创建了“Loading ...”,其中名称将在左侧。

var player_obj = models.Playlist.fromURI('spotify:user:diannallm:playlist:7A5y9BA7dxQfOdEoN8igbY');

1 个答案:

答案 0 :(得分:1)

终于想通了。

player_obj.load('tracks').done(function(po){
    models.Playlist.create(player_obj.name).done(function(new_playlist) {
        new_playlist.load('tracks').done(function(new_playlist_tracks) {
            po.tracks.snapshot().done(function(tracksnapshot){
                new_playlist_tracks.tracks.add(tracksnapshot.toArray());
                /*
                //This also works, but is less efficient and tracks may be entered out of order
                for (var i=0; i<tracksnapshot.length; i++){
                    var track = tracksnapshot.get(i);
                    new_playlist_tracks.tracks.add(track);
                }
                */
            });
        });
    });
});