Spotify App API: Play specific track from album

2020-05-08 18:36发布

问题:

Trying to play a specific track from an album with the player view. It works all good when using

player.play(objTrack, objTrack.album);

But I don't want the song to autoplay, so I'm using:

player.track = objTrack;
player.context = objTrack.album;

But when I do so, either it won't work at all or it will start playing the first track of the album regardless which track I set.

How can I make this work? :/ Thanks.


This does not do the trick either. I'm currently trying this method:

var album = m.Album.fromURI('spotify:album:7Kmmw7Z5D2UD5MVwdm10sT', function(album) {
   var player = new v.Player();
   player.track = album.get(3);
   player.context = album;
   document.body.appendChild(player.node);
});

But this will not fetch and play track number 3, it will play the first track of the album. And if the album only contains 1 track, it will crash. Any ideas?

回答1:

If you have the current track number I suppose you got it from a track object. Here is my way:

// trackObj <- The track object

var album = m.Album.fromURI('spotify:album:7Kmmw7Z5D2UD5MVwdm10sT', function(album) {
    var player = new v.Player();
    player.track = trackObj;
    album.get = function() {
        return trackObj;
    }
    player.context = album;
    document.body.appendChild(player.node);
});


标签: api spotify