How can I tell if a song has ended or the next but

2019-08-12 12:22发布

问题:

I'm using the Spotify API to develop an app for the desktop Spotify application on OSX. I've been beating my head against the wall trying to figure out how to tell if a song has ended and trigger another track to play.

Here's my event listener code:

models.player.observe(models.EVENT.CHANGE, function(event) {
    console.debug(event);
    if(event.data.playstate == false){
        $('.wholikes').remove();
        getTrack();
    }               
});

I guess I need to be looking at event.data, but when i check the inspector it has the same properties no matter what change it is observing. The type is set to 'playerStateChanged' which is not specific. I'm looking to trigger a new track at the end of a song or when the next button is pressed, but not when a user changes the scrubber to a different part of the song. Any ideas? I've check the documentation thoroughly and can't find this anywhere.

回答1:

When the event you're observing fires, check the player object's track property (models.player.track). When the track changes, you should find this changed to the next track if there is one, or null. At this point, you can start the next track.