MichaelStoddart MichaelStoddart - 6 months ago 29
Android Question

Getting and playing 30 second previews from Spotify

I've looked into how to play 30 second previews but so far all I can find is android wrappers for the web API that require user authentication.

I need to be able to search for an artist and play the first preview that pops up without needing the user to authenticate their accounts


So in order to get this working i had to chain 2 API calls together. The first one searches for the artist and then the second one looks for the preview:

You call the API with this link (GET): https://api.spotify.com/v1/search?q=<artist name>&type=artist

which then returns a json structure like this:

    "artists": {
    "href": "https://api.spotify.com/v1/search?query=bring+me+the+horizon&offset=0&limit=20&type=artist",
    "items": [
    "external_urls": {
    "spotify": "https://open.spotify.com/artist/1Ffb6ejR6Fe5IamqA5oRUF"
    "followers": {
    "href": null,
    "total": 1067846
    "genres": [
    "href": "https://api.spotify.com/v1/artists/1Ffb6ejR6Fe5IamqA5oRUF",
    "id": "1Ffb6ejR6Fe5IamqA5oRUF",
    "images": [
    "height": 640,
    "url": "https://i.scdn.co/image/49aad7da4f872acb3005727392631dab282423d1",
    "width": 640
    "height": 320,
    "url": "https://i.scdn.co/image/d9cf89b9db73b95ed15d9e29e30d0dd8afea23e2",
    "width": 320
    "height": 160,
    "url": "https://i.scdn.co/image/d9e514e15f4940c77029ef3b11291d557b345ae9",
    "width": 160
    "name": "Bring Me The Horizon",
    "popularity": 76,
    "type": "artist",
    "uri": "spotify:artist:1Ffb6ejR6Fe5IamqA5oRUF"
    "limit": 20,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 1

Because this then returns the Artist ID we can get the top tracks for that artist and play a preview:

Call the API with this URL (GET):

https://api.spotify.com/v1/artists/<ARTIST ID>/top-tracks?country=GB

From here we can then extract the URL of the preview and play it, all without authentication!