2

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.

When I try to do to play with a correct uri sp_track_error() returns SP_ERROR_IS_LOADING,

metadata_updated never gets called, and of course the program hangs.I have check many uri

and get the same result.

Did I miss something or misunderstand the APIs?

This is the main loop:

    pthread_mutex_lock(&g_notify_mutex);
    for(;;)
    {

        if (next_timeout == 0)
        {
            while(!g_notify_do && !g_playback_done)
            {
                pthread_cond_wait(&g_notify_cond, &g_notify_mutex);
            }

        }
        else
        {
            struct timespec ts;

#if _POSIX_TIMERS > 0
            clock_gettime(CLOCK_REALTIME, &ts);
#else
            struct timeval tv;
            gettimeofday(&tv, NULL);
            TIMEVAL_TO_TIMESPEC(&tv, &ts);
#endif
            printf("%d\n",next_timeout);
            if((ts.tv_nsec+(next_timeout % 1000) * 1000000)>=1000000000)
            {
                ts.tv_nsec += (next_timeout % 1000) * 1000000-1000000000;
                ts.tv_sec += next_timeout / 1000+1;
            }
            else
            {
                ts.tv_sec += next_timeout / 1000;
                ts.tv_nsec += (next_timeout % 1000) * 1000000;
            }
            pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts);
        }

        g_notify_do = 0;
        pthread_mutex_unlock(&g_notify_mutex);

         g_currenttrack= sp_link_as_track(sp_link_create_from_string(spotify:track:1NrJYpdAi7uosDRPmSYrsG));
         sp_track_add_ref(g_currenttrack);

         if (sp_track_error( g_currenttrack) == SP_ERROR_OK) {

                sp_session_player_load(g_sess, g_currenttrack);
                sp_session_player_play(g_sess, 1);
          }
        do
        {
            sp_session_process_events(g_sess, &next_timeout);
        }
        while (next_timeout == 0);

        pthread_mutex_lock(&g_notify_mutex);
}

I found that metadata_update called by main loop,but when the track has been created this loop will hang out for a long time(about 290s).

  • Do you have an event loop call sp_session_process_events as required? – Weeble Aug 08 '13 at 07:07
  • Of course.And all the action above is in the loop,am I right? – facebook-100001054243509 Aug 08 '13 at 07:30
  • That sounds right. I think you can and should release the sp_link after you've added a reference to the sp_track, but that shouldn't stop your track playing, it just means you might leak resources. I find step 4 a little hard to follow - perhaps you're missing a word there? Could you clarify when all this occurs relative to your main loop? Are you calling these functions during a callback from libspotify? Are you saying that it worked for some track uris but not others, or that it never works at all? – Weeble Aug 08 '13 at 08:14
  • I have add the code of main loop.Any uri return SP_ERROR_IS_LOADING,and metadata_updated never called. – facebook-100001054243509 Aug 08 '13 at 08:50

1 Answers1

0

metadata_updated won't be called unless sp_session_process_events() is called. The reason your code is stalling for 300s is because after the login, sp_session_process_events() sets the next call timeout to 300s. After step 3, you could force your own notify main thread, and this would fix the issue. Unfortunately I don't know why libspotify does not send a notify main thread at this point. I guess we're both missing something here.

Bishan
  • 15,211
  • 52
  • 164
  • 258
GrantTheAnt
  • 871
  • 8
  • 11