mile
mile

Reputation: 422

libspotify: how to find out if a track uri is correct and playable?

The question sounds very simple but I couldn't find a way to check if a track uri is correct.

For example, the normal procedure to play a track by a given valid track uri spotify:track:5Z7ygHQo02SUrFmcgpwsKW is:

1) get sp_link* by sp_link_create_from_string(const char *$track_uri)

2) get sp_track* by sp_link_as_track(sp_link*)

3) sp_track_add_ref(sp_track*)

4) if sp_track_error() returns SP_ERROR_OK, or SP_ERROR_IS_LOADING but metadata_updated and SP_ERROR_OK then sp_session_player_load and sp_session_player_play to load and play the track.

5) sp_track_release() and sp_session_player_unload() when it's the end of track.

Now if user made a typo on the spotify track uri and tried to play spotify:track:5Z7ygHQo02SUrFmcgpwsKQ (the last character Q is a typo, should be W). All the possible error checks during the procedure don't return any error:

step 1) sp_link is not NULL

step 2) sp_track* is not NULL

step 3) returns SP_ERROR_OK

step 4) sp_track_error() returns SP_ERROR_IS_LOADING, metadata_updated never gets called, and of course the program hangs.

The checks before the track is loaded can't find out the invalidity of a track uri. The API sp_track_availability sounds like a way, but it will always return NOT AVAILABLE if the track is not loaded, but if the track uri is not correct, track will never be loaded.

Did I miss something or misunderstand the APIs?

Upvotes: 3

Views: 1057

Answers (2)

NOUX
NOUX

Reputation: 201

copy the track uri spotify:track:5Z7ygHQo02SUrFmcgpwsKW to the address bar of browser, and Enter, it will automatically switch to spotify app and locate to that album.

Upvotes: 0

hugolm84
hugolm84

Reputation: 166

You can check if the uri is valid by checking the link type which states:

enum    sp_linktype { 
  SP_LINKTYPE_INVALID = 0, 
  SP_LINKTYPE_TRACK = 1, 
  SP_LINKTYPE_ALBUM = 2, 
  SP_LINKTYPE_ARTIST = 3, 
  SP_LINKTYPE_SEARCH = 4, 
  SP_LINKTYPE_PLAYLIST = 5, 
  SP_LINKTYPE_PROFILE = 6, 
  SP_LINKTYPE_STARRED = 7, 
  SP_LINKTYPE_LOCALTRACK = 8, 
  SP_LINKTYPE_IMAGE = 9 
}

That leaves us possibilities to perform link checks like so:

  1. check if sp_link_type(link) == SP_LINKTYPE_TRACK
  2. create a sp_track* track from sp_link_as_track(link), or, sp_track_get_playable(sp_link*, sp_session*)
  3. wait for it to be loaded, then check availability

Upvotes: 2

Related Questions