I’m working on a Spotify app and need to control which tracks get played. I want to disable the skip forward and skip backward buttons so users can’t navigate away from the current song.
From what I understand, the solution involves playing tracks without providing any context to the player. I tried this approach but the skip buttons remain active. It looks like Spotify is still using my favorites playlist as the default context.
Here’s my current implementation:
var spotifyAPI = getSpotifyApi(1);
var apiModels = spotifyAPI.require('sp://import/scripts/api/models');
var audioPlayer = apiModels.player;
var favoritesList = apiModels.library.starredPlaylist;
exports.initialize = initialize;
function initialize() {
var titleElement = document.getElementById("title");
var randomIndex = Math.floor(Math.random()*favoritesList.length)
var selectedTrack = favoritesList.get(randomIndex);
if (favoritesList == null) {
titleElement.innerText = "favorites not loaded";
} else if (selectedTrack == null) {
titleElement.innerHTML = "failed to get track from favorites";
} else {
titleElement.innerHTML = "favorites " + randomIndex + "/" + favoritesList.length + " : " + selectedTrack.name;
}
audioPlayer.play(selectedTrack, null);
}
I also attempted to load tracks using direct URIs without accessing the favorites playlist:
function initialize() {
var titleElement = document.getElementById("title");
var audioPlayer = apiModels.player;
apiModels.Track.fromURI("spotify:track:4uLU6hMCjMI75M1A2tKUQC", function(isolatedTrack) {
console.log("Track retrieved", isolatedTrack.name);
audioPlayer.play(isolatedTrack, null);
titleElement.innerHTML = "now playing: " + isolatedTrack.name;
});
}
Interestingly, the skip buttons aren’t completely disabled. When I click previous it restarts the current track, and clicking next moves to an empty state where all controls become inactive. Has anyone found a working solution for this?