问题
I want to be able to disable the Next and the previous buttons in spotify. I know it's possible because the soundrop app has managed to do it.
I have searched around a bit, and from my understanding, I am supposed to play a track with the context as null or a context containing only 1 track. I have been testing around with all the possible solutions I can think of, but none of them seems to work. Most of them actually result in the track being played with context as null, but the Next/Previous buttons are always enabled.
If I press the next button, the player ends up not playing any track, and both buttons are disabled...
Below is the code from my test application.
var sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');
var views = sp.require('sp://import/scripts/api/views');
models.player.observe(models.EVENT.CHANGE, function (o) {
console.log(['Player Change', o, models.player.context]);
})
$('#play1').click(function () {
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
console.log(["play1", track.name]);
models.player.play(track, null);
});
});
$('#play2').click(function () {
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
console.log(["play2", track.name]);
var p = new views.Player();
p.play(track, null);
});
});
$('#play3').click(function () {
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
console.log(["play3", track.name]);
var pl = new models.Playlist();
pl.add(track);
models.player.play(track, pl);
});
});
$('#play4').click(function () {
console.log(["play4"]);
models.player.play("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", null);
});
$('#play5').click(function () {
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
models.player.playTrack(track);
});
});
$('#play6').click(function () {
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
console.log(["play6", track.name]);
var pl = new models.Playlist();
pl.add(track);
models.player.playTrackWithContext(track, pl, 0);
});
});
$('#play7').click(function () {
var l = new models.Link("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef");
sp.trackPlayer.setContextCanSkipNext(l, false);
console.log(["play7", models.player, sp.trackPlayer],l);
models.player.play(l, null);
});
Result from #play1:
["play1", "Jag är här nu"]
["Player Change", Object, null]
Result from #play2:
["play2", "Jag är här nu"]
["Player Change", Object, null]
Result from #play3:
["play3", "Jag är här nu"]
["Player Change", Object, "spotify:internal:temp_playlist:spotify:app:myapplication@687556942764"]
Result from #play4:
["play4"]
["Player Change", Object, null]
Result from #play5:
["play5", "Jag är här nu"]
["Player Change", Object, null]
Result from #play6:
["play6", "Jag är här nu"]
["Player Change", Object, "spotify:internal:temp_playlist:spotify:app:myapplication@217702072872"]
Result from #play7:
sp.trackPlayer.setContextCanSkipNext(l, false);
The above line generates an exception with the message "Failed to convert param 0 to link"
Thanks in advance
回答1:
I've after some work now, found out how to do this.
models.Track.fromURI("spotify:track:5A1fNC7Fdb0yyh3UA6T1ef", function (track) {
var pl = new models.Playlist();
sp.trackPlayer.setContextCanSkipPrev(pl.uri, false);
sp.trackPlayer.setContextCanSkipNext(pl.uri, false);
pl.add(track);
models.player.play(track, pl);
});
Hope this helps anyone else out there attempting to resolve this issue and getting stuck in the talk about playing a track with an empty context...
来源:https://stackoverflow.com/questions/11293430/disable-next-previous-buttons-in-spotify