Pick an artist's album from a dialog and play it in Google Play Music

Learn how to query the Spotify API for albums of a given artist and then play that album on GPM

  1. joaomgcd
    In this project we'll
    • Do a voice command asking for albums for a certain artist
    • Query the Spotify API for albums from that artist
    • Pick an album from a list dialog
    • Play that album on Google Play Music
    This is how it'll look like when it's done:


    STEP 0 - SET DEFAULT ARTIST TO TEST


    (i) This step's only purpose is to make the task easier to test to check if everything's working correctly. It'll set the %artist variable to a value only if the variable is not already set. By doing so, it'll always work with the same test artist while testing but will also work correctly in the final, real situation.

    • Create a Task and add a Variable Set action
    • Set %artist to any artist you like, like Muse for example
    • Set the If condition to only set the variable if it isn't already set


    STEP 1 - GET ARTIST ID FROM SPOTIFY


    (i) This will query the Spotify Search API for artists with the given name and get back the response in the JSON format which AutoTools will then read
    This is a sample of how the returned JSON text will look like:
    Code (Text):
    {
        "artists": {
            "href": "https://api.spotify.com/v1/search?query=Muse&offset=0&limit=20&type=artist",
            "items": [
                {
                    "external_urls": {
                        "spotify": "https://open.spotify.com/artist/12Chz98pHFMPJEknJQMWvI"
                    },
                    "followers": {
                        "href": null,
                        "total": 2280046
                    },
                    "genres": [
                        "alternative metal",
                        "alternative rock",
                        "garage rock",
                        "indie rock",
                        "permanent wave",
                        "piano rock",
                        "post-grunge",
                        "rock"
                    ],
                    "href": "https://api.spotify.com/v1/artists/12Chz98pHFMPJEknJQMWvI",
                    "id": "12Chz98pHFMPJEknJQMWvI",
                    "images": [
                        {
                            "height": 667,
                            "url": "https://i.scdn.co/image/0b3c04473aa6a2db8235e5092ec3413f35752b8d",
                            "width": 1000
                        }
                    ],
                    "name": "Muse",
                    "popularity": 77,
                    "type": "artist",
                    "uri": "spotify:artist:12Chz98pHFMPJEknJQMWvI"
                }
            ],
            "limit": 20,
            "next": "https://api.spotify.com/v1/search?query=Muse&offset=20&limit=20&type=artist",
            "offset": 0,
            "previous": null,
            "total": 133
        }
    }
    • Set the Fields field to id
    (i) As you can see in the JSON above, there's a field with the name id which contains the artist ID in the Spotify API. By using id in the Fields field you're telling AutoTools to get the very first value for any field called id in the JSON.
    • Set the Variable Name field under Advanced to artistid
    (i) Because Tasker doesn't support variables names with just 2 letters (which is the case with id) you need to give it a longer name
    • Go back to Tasker and add a Flash action with the text %artistid
    • Check that it's going well so far by running the task and see that an ID flashes on the screen


    STEP 2 - GET ALBUM NAMES


    (i) This will query the Spotify artist albums API for albums from the artist with the specified ID, which are of type album and are available in the US market
    • Set the Fields field to name()
    (i) By using () in the Fields field, you're telling AutoTools to get all available values for fields that are called name in the artist's albums JSON response. This is a sample response. Check how there's a name field for each album:
    Code (Text):

    {
      "href" : "https://api.spotify.com/v1/artists/12Chz98pHFMPJEknJQMWvI/albums?offset=0&limit=20&album_type=album&market=US",
      "items" : [ {
        "album_type" : "album",
        "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/2wart5Qjnvx1fd7LPdQxgJ"
        },
        "href" : "https://api.spotify.com/v1/albums/2wart5Qjnvx1fd7LPdQxgJ",
        "id" : "2wart5Qjnvx1fd7LPdQxgJ",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/849eecf3c9df835181c2970c435ac2d008346ea3",
          "width" : 640
        }],
        "name" : "Drones",
        "type" : "album",
        "uri" : "spotify:album:2wart5Qjnvx1fd7LPdQxgJ"
      }],
      "limit" : 20,
      "next" : null,
      "offset" : 0,
      "previous" : null,
      "total" : 10
    }
     
    • Go back to Tasker and add a Flash action with the text %name()
    • Run the Task and check that first an ID is flashed and then a list of album names is flashed


    STEP 3 - SELECT ALBUM


    • Add an AutoTools Dialog action
    • Set the Dialog Type field to List
    • Set the Title field to %artist Albums
    • Set the Texts field to %name()
    (i) This will create a list dialog where each line is the name of an album. Because no commands were set, when a line is selected, the name of the selected album will be available in the %atcommand variable. If the Commands field would be set, the corresponding command would be available in the variable.
    • Go back to Tasker and add a Flash action with the text %atcommand
    • Run the task to check if all flashes appear correctly


    STEP 4 - PLAY ALBUM IN GOOGLE PLAY MUSIC


    • Add an AutoShare action
    • Import the Google Play Music Search And Play Album intent
    • Set the App field to Media
    • Set the Action field to Search And Play Album
    • Set the Album field to %atcommand
    • Tap the Find Compatible apps option and select Google Play Music
    • Go back to Tasker and run the task to see if everything's working


    STEP 5 - MAKE TASK CALLABLE WITH A VOICE COMMAND


    • Create a new profile with the AutoVoice Recognized event condition
    • Set the command filter to show albums by (?<artist>.+)
    • Check the Regex option
    • As the entry task select the task that you created above
    • Exit Tasker to save your configuration


    STEP 6 - TEST WITH VOICE COMMAND


    If you now do an AutoVoice command like Show albums by Muse it'll show you a list of album names and when you select one it'll play it on Google Play Music