link.c

The link.c example shows how you can use the link handling functions.

#include <stdio.h>
#include <spotify/api.h>


/* --- Data --- */
extern int g_exit_code;


static const char* get_link_type_label(sp_link *link)
{
    static const char *LINK_TYPES[] = {
        "invalid",
        "track",
        "album",
        "artist",
        "search",
        "playlist"
    };

    return LINK_TYPES[sp_link_type(link)];
}

static void print_link(sp_link *link)
{
    char spotify_uri[256];

    if (0 > sp_link_as_string(link, spotify_uri, sizeof(spotify_uri))) {
        fprintf(stderr, "failed to render Spotify URI from link\n");
        g_exit_code = 8;
        return;
    }

    printf("%s link %s\n", get_link_type_label(link), spotify_uri);
}

static void try_links(sp_session *session)
{
    const char SPOTIFY_URI[] = "spotify:track:6JEK0CvvjDjjMUBFoXShNZ";
    sp_link *link = sp_link_create_from_string(SPOTIFY_URI);

    if (!link) {
        fprintf(stderr, "failed to get link from a Spotify URI\n");
        g_exit_code = 6;
        return;
    }

    print_link(link);

    // The create function will have increased the reference count for us.
    sp_link_release(link);
}

void metadata_updated(sp_session *session)
{
}

void session_ready(sp_session *session)
{
    sp_error error;

    try_links(session);
    error = sp_session_logout(session);

    if (SP_ERROR_OK != error) {
        fprintf(stderr, "failed to log out from Spotify: %s\n",
                        sp_error_message(error));
        g_exit_code = 5;
        return;
    }
}

void session_terminated(void)
{
}

Generated on Tue Apr 7 15:21:55 2009.
Copyright © 2006–2009 Spotify Ltd