Getting started with Android SDK Beta
Introduction
This tutorial leads you step-by-step through the creation of a simple app that uses the Spotify App Remote SDK to play a playlist. We show you how to:
- Play a playlist from a URI
- Subscribe to PlayerState of the Spotify app and read its data
If you are new to developing Android apps, we recommend reading the tutorials on Google's Android developer portal.
The Spotify Platform can not be used to develop commercial streaming integrations.
More informationThe Spotify Platform can not be used to develop applications that alter Spotify Content.
More informationYou may not synchronize any sound recordings with any visual media, including any advertising, film, television program, slideshow, video, or similar content
More informationThe Spotify Platform can not be used for non-interactive broadcasting.
More information
Prepare Your Environment
Register Your App
You will need to register your application on the Developer Dashboard and obtain a client ID. When you register your app you will also need to whitelist a redirect URI that the Spotify Accounts Service will use to callback to your app after authorization.
You also should add your package name and app fingerprint as they're used to verify the identity of your application.
Select "Android" for the question asking which APIs are you planning to use.
Install Spotify App
App Remote SDK requires the Spotify app to be installed on the device. Install the latest version of Spotify from Google Play on the device you want to use for development. Run the Spotify app and login or sign up.
Download the SDK
Download the Spotify Android SDK from our GitHub.
Create Your App
Create or make sure you have an Android app with at least one Activity or Service in which you can put your code to connect to Spotify.
Edit your MainActivity
to look like this:
Add the App Remote SDK
Unzip the App Remote SDK zip file that you downloaded. Add the library to your project by importing it as a module. In the Project side bar in Android Studio (View > Tool Windows > Project), right click your project's root folder and navigate to New > Module.
In the New Module window, choose the option Import .JAR/AAR Package. Click Next.
Press the "..." button and locate the spotify-app-remote-release-version.aar
under the app-remote-lib folder in the unzipped bundle. Click Open and
choose a suitable subproject name. We're using spotify-app-remote in this
example. Click Finish to import the .aar into your project.
Add a dependency on the spotify-app-remote module to your app by adding the imported subproject and Gson to your app's build.gradle file. It is important that the name that you specify in the build.gradle file is the same as the subproject name you decided on.
_10dependencies {_10 // your app dependencies_10 implementation project(':spotify-app-remote')_10 implementation "com.google.code.gson:gson:2.6.1"_10}
Since version 0.2.0 of the App Remote SDK, Gson is used by default for serializing and deserializing the request. Jackson is also supported. If you would like to use Jackson instead please see the FAQ here.
Import Dependencies
_10import com.spotify.android.appremote.api.ConnectionParams;_10import com.spotify.android.appremote.api.Connector;_10import com.spotify.android.appremote.api.SpotifyAppRemote;_10_10import com.spotify.protocol.client.Subscription;_10import com.spotify.protocol.types.PlayerState;_10import com.spotify.protocol.types.Track;
Set up your Spotify credentials
Authorize Your Application
To be able to use the App Remote SDK the user needs to authorize your
application. If they haven't, the connection will fail
with UserNotAuthorizedException
. To allow the user to authorize your app, you
will use the built-in authorization described below.
Authorizing the user using the built-in auth flow
For this use-case, you don't have to add the Authorization Library
to your application. You can request that the authorization view is shown to
users who have not approved the app-remote-control
scope by passing the flag
showAuthView
in the ConnectionParams
. The scope is automatically requested
for you by the library.
Add the following to your onStart
method:
Built-in auth provides offline support. This means that a user can be authorized even if the device is currently offline. Offline support works out of the box, so it doesn't require any additional implementation.
To successfully authorize a user while offline, the following conditions have to be met:
- Your application has successfully connected to Spotify within the last 24 hours
- Your application uses the same redirect URI, client ID and scopes when connecting to Spotify
Authorizing user with Single Sign-On library
Download and add Authorization Library to your project as described in this guide.
Create and send the authorization request as described in the Android SDK Authorization Guide. You need the app-remote-control
scope to use the Spotify App Remote SDK.
Connect to App Remote
The first thing we need to do is to use the SpotifyAppRemote.Connector
to
connect to Spotify and get an instance of SpotifyAppRemote
. To do this, we
call the connect
method using the connectionParams
we defined above. Add
the following to your onStart
method.
Play a Playlist
To play a playlist given a Spotify playlist URI, we are going to connect to the
Spotify app and use the PlayerApi
command. From there we can get the
PlayerApi
directly and call play. Add the following to your private
connected
method:
Run the application and you should hear some feel-good indie tunes playing on your phone. If you prefer
something more relaxing, why not try some sweet piano music spotify:playlist:37i9dQZF1DX7K31D69s4M1
. If you
don't hear music playing and end up in the onFailure
callback above, please read up on connection errors and try again.
Subscribe to PlayerState
The PlayerApi
offers, in addition to the play(uri)
method we use above, the ability to subscribe
to and poll for the state of the Spotify player. Add the following to your code to subscribe to PlayerState
and log the track title and artist of the song that will be playing:
Run the app again. You should now see a log with the track name and artist of the currently playing track.
Disconnecting from App Remote
Don't forget to disconnect from App Remote when you no longer need it. Add the following to your onStop
method.
Next Steps
Congratulations! You've interacted with the App Remote SDK for the first time. Time to celebrate, you did a great job! 👏
Want more? Here's what you can do next:
-
Learn about best practices for handling App Remote SDK interactions and connections in your Android application.
-
Dive into other things you can do with the SDK in the App Remote SDK Reference.
Source Code
The Quick Start source code is below. Copy into your Main Activity.