1

I'm trying to get familiar with libspotify and I must say the documentation on libspotify is seriously lacking. I've hacked together a small app from the examples but I can't get it to work.

I'm building a C Console Application in Visual Studio 2012. The appkey is correct.

sp_session_config config;
sp_error error;
sp_session *session;
char *blob = NULL;

memset(&config, 0, sizeof(config));

config.api_version = SPOTIFY_API_VERSION;
config.cache_location = "tmp";
config.settings_location = "tmp";
config.application_key = g_appkey;
config.application_key_size = g_appkey_size;

config.user_agent = "SpotiTest";

error = sp_session_create(&config, &session);
if (SP_ERROR_OK != error) {
    fprintf(stderr, "failed to create session: %s\n",
                    sp_error_message(error));
    return;
}

error = sp_session_login(session, "USERNAME", "PASSWORD", 1, blob);
if (SP_ERROR_OK != error) {
    fprintf(stderr, "failed to log in to Spotify: %s\n",
                    sp_error_message(error));
    sp_session_release(session);
    exit(4);
}

sp_connectionstate cs = sp_session_connectionstate (session);

No matter what the username and password (false or correct) sp_session_login always returns SP_ERROR_OK. When I check the connection state with sp_session_connectionstate it always returns SP_CONNECTION_STATE_LOGGED_OUT.

I'm not seeing what I'm doing wrong here and also can't seem to find any relevant answers through the regular channels.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Demigod
  • 11
  • 3

1 Answers1

1

The API is asynchronous. sp_session_login returns immediately, and the login process continues in the background. Look at the examples that come with the API. You need some kind of event loop to call sp_session_process_events, or libspotify won't get any work done, and you probably want to wait until you receive the logged_in callback.

Weeble
  • 17,058
  • 3
  • 60
  • 75
  • So to be sure. Should I call sp_session_process_events in the idle loop all the time, like: while(1) { sp_session_process_events(); } Or should I only call it when [code]notify_main_thread[/code] callback is called? – Demigod Jan 19 '13 at 09:52
  • It's slightly more complicated than that. Look at the examples that come with libspotify, such as spshell. Whenever you call `sp_session_process_events`, it will return a time in milliseconds to wait before calling it again. You should wait until either that time elapses or `notify_main_thread` is called, whichever comes first. If you just spin calling `sp_session_process_events` over and over it will waste a lot of CPU time and might stop other things from working. – Weeble Jan 21 '13 at 09:28