I’m working with the Spotify Web API and trying to set up a specific song from an album to be ready for playback, but I don’t want it to start playing automatically.
When I use the direct play method, everything works fine:
audioPlayer.play(selectedSong, selectedSong.albumData);
However, since I want to prepare the track without immediate playback, I’m trying this approach:
audioPlayer.track = selectedSong;
audioPlayer.context = selectedSong.albumData;
The problem is that this either fails completely or defaults to playing the album’s first track instead of my chosen song.
I also attempted this alternative method:
var albumData = m.Album.fromURI('spotify:album:2Bxkw9Z3D1UD7MVwdm12sR', function(albumData) {
var audioPlayer = new v.Player();
audioPlayer.track = albumData.get(2);
audioPlayer.context = albumData;
document.body.appendChild(audioPlayer.node);
});
But this still loads the first track rather than track number 2, and crashes when the album has only one song. Any suggestions on how to properly queue a specific track position?
I’ve hit this same issue with Spotify’s Web API. Setting the track property directly doesn’t work reliably - the Player needs proper initialization through the play method. Here’s what fixed it for me: use the play method with the play parameter set to false (not officially documented, but it works). Or initialize the player with your track and immediately pause it. Try this: call audioPlayer.play(selectedSong, selectedSong.albumData) then immediately audioPlayer.pause() in the callback. This loads the track properly without auto-playing. That crash with single-track albums? You’re accessing an index that doesn’t exist. Always check the album’s track count before accessing specific indices.
The problem is Spotify’s Player expects tracks loaded through its state machine, not by setting properties directly. Here’s what fixed it for me: use addToQueue(selectedSong.uri) first, then audioPlayer.seek(0) without calling play. This queues your track and positions it right without auto-playing. If addToQueue isn’t available in your API version, create a custom playlist with just that song and load the playlist instead of the full album. Those track index crashes? You’re forgetting zero-based indexing - track 2 is index 1, and single-track albums only have index 0.
Been there! Skip setting track directly and use the load() method instead. Try audioPlayer.load(selectedSong, selectedSong.albumData) - preloads without autoplay. Double-check your selectedSong object structure too, the API sometimes returns different formats depending on how you fetch it.
Direct property assignment won’t work here - the Player class validates context internally before setting track position. Try using the prepare() method if your API version has it, or create a modified play call with autoStart disabled. First check if your audioPlayer supports audioPlayer.prepare(selectedSong, selectedSong.albumData). If not, override the default behavior by setting player state before calling play: audioPlayer.autoStart = false; audioPlayer.play(selectedSong, selectedSong.albumData);. You’re getting crashes because of hardcoded indices - always validate with albumData.tracks.length before accessing specific positions. I’ve used this pattern consistently and it avoids the timing issues you get with pause() workarounds.
Been there. Spotify’s player ignores manual track/context assignment. Use audioPlayer.cue() instead - that’s what it’s for. No cue method? Initialize empty, then audioPlayer.setTrack(selectedSong, false) (false stops autoplay). Way cleaner than the pause hack.