我尝试使用以下代码在Spotify应用中显示简单的播放列表视图:
sp = getSpotifyApi(1);
var m = sp.require("sp://import/scripts/api/models");
var v = sp.require("sp://import/scripts/api/views");
var jq = sp.require('sp://XXX/jquery/jquery-1.7.1.min');
var pl = m.Playlist.fromURI('spotify:user:d3marcus:playlist:4zPZzImEYkUOVBvxIo42im');
var player = new v.Player();
player.track = pl.get(0);
player.context = pl;
var list = new v.List(pl);
$('XXX').append(list.node);
这将导致空列表视图和 sp://import/scripts/language.js:44 中捕获的错误:"未捕获的TypeError:无法读取属性'长度'未定义"
有什么建议吗?
答案 0 :(得分:1)
我会说您收到此错误,因为当您执行pl.get(0)
时播放列表尚未加载。要确保已加载播放列表模型,您可以执行
pl = m.Playlist.fromURI('spotify:user:d3marcus:playlist:4zPZzImEYkUOVBvxIo42im');
pl.observe(models.EVENT.LOAD, function() {
player.track = pl.get(0);
...
});
或
m.Playlist.fromURI("spotify:user:d3marcus:playlist:4zPZzImEYkUOVBvxIo42im", function(pl) {
player.track = pl.get(0);
...
});
答案 1 :(得分:0)
我不确定,但你能尝试一下吗?
$('YYY').append($(player.node));
$('XXX').append($(list.node));
而不是
$('XXX').append(list.node);
告诉我们......
答案 2 :(得分:0)
对于1.0 API:
require([
'$api/models',
'$views/list#List'
], function (models,List) {
var addList = function(list) {
list.load('tracks').done(function(list) {
list.tracks.snapshot().done(function(trackSnapshot){
// Make the playlist view
var multiple_tracks_player = document.getElementById('addedTracksList');
var playableList = List.forPlaylist(list);
multiple_tracks_player.appendChild(playableList.node);
playableList.init();
});
});
}
exports.addList = addList;
}
// Example of use:
addList(models.Playlist.fromURI(...))
我已经按照上面的使用进行了测试,所以它应该可行。
我在“播放音乐”部分的tutorial-app available on github下找到了这个部分 - > “播放曲目列表”
我希望这有用。