Beta
IntroductionThis 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:
If you are new to developing Android apps, we recommend reading the tutorials on Google's Android developer portal.
Important policy notes
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 informationYou 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 AppApp 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 SDKDownload the Spotify Android SDK from our GitHub.
Create Your AppCreate 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:
_21
class MainActivity : AppCompatActivity() {
_21
override fun onCreate(savedInstanceState: Bundle?) {
_21
super.onCreate(savedInstanceState)
_21
setContentView(R.layout.activity_main)
_21
override fun onStart() {
_21
// We will start writing our code here.
_21
private fun connected() {
_21
// Then we will write some more code here.
_21
override fun onStop() {
_21
// Aaand we will finish off here.
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.
_10
// your app dependencies
_10
implementation project(':spotify-app-remote')
_10
implementation "com.google.code.gson:gson:2.6.1"
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
_10
import com.spotify.android.appremote.api.ConnectionParams;
_10
import com.spotify.android.appremote.api.Connector;
_10
import com.spotify.android.appremote.api.SpotifyAppRemote;
_10
import com.spotify.protocol.client.Subscription;
_10
import com.spotify.protocol.types.PlayerState;
_10
import com.spotify.protocol.types.Track;
Set up your Spotify credentials
_10
private val clientId = "your_client_id"
_10
private val redirectUri = "http://com.yourdomain.yourapp/callback"
_10
private var spotifyAppRemote: SpotifyAppRemote? = null
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.
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:
_10
// Set the connection parameters
_10
val connectionParams = ConnectionParams.Builder(clientId)
_10
.setRedirectUri(redirectUri)
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:
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.
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.
_13
SpotifyAppRemote.connect(this, connectionParams, object : Connector.ConnectionListener {
_13
override fun onConnected(appRemote: SpotifyAppRemote) {
_13
spotifyAppRemote = appRemote
_13
Log.d("MainActivity", "Connected! Yay!")
_13
// Now you can start interacting with App Remote
_13
override fun onFailure(throwable: Throwable) {
_13
Log.e("MainActivity", throwable.message, throwable)
_13
// Something went wrong when attempting to connect! Handle errors here
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:
_10
spotifyAppRemote.playerApi.play("spotify:playlist:37i9dQZF1DX2sUQwD7tbmL")
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.
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:
_10
// Subscribe to PlayerState
_10
spotifyAppRemote.playerApi.subscribeToPlayerState().setEventCallback {
_10
val track: Track = it.track
_10
Log.d("MainActivity", track.name + " by " + track.artist.name)
Run the app again. You should now see a log with the track name and artist of the currently playing track.
Disconnecting from App RemoteDon't forget to disconnect from App Remote when you no longer need it. Add the following to your onStop
method.
_10
override fun onStop() {
_10
spotifyAppRemote?.let {
_10
SpotifyAppRemote.disconnect(it)
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.
The Quick Start source code is below. Copy into your Main Activity.
_70
package com.yourdomain.yourapp;
_70
import android.os.Bundle;
_70
import android.support.v7.app.AppCompatActivity;
_70
import android.util.Log;
_70
import com.spotify.android.appremote.api.ConnectionParams;
_70
import com.spotify.android.appremote.api.Connector;
_70
import com.spotify.android.appremote.api.SpotifyAppRemote;
_70
import com.spotify.protocol.client.Subscription;
_70
import com.spotify.protocol.types.PlayerState;
_70
import com.spotify.protocol.types.Track;
_70
class MainActivity : AppCompatActivity() {
_70
private val clientId = "ad0911afa57949bba362003f601876b2"
_70
private val redirectUri = "https://com.spotify.android.spotifysdkkotlindemo/callback"
_70
private var spotifyAppRemote: SpotifyAppRemote? = null
_70
override fun onCreate(savedInstanceState: Bundle?) {
_70
super.onCreate(savedInstanceState)
_70
setContentView(R.layout.activity_main)
_70
override fun onStart() {
_70
val connectionParams = ConnectionParams.Builder(clientId)
_70
.setRedirectUri(redirectUri)
_70
SpotifyAppRemote.connect(this, connectionParams, object : Connector.ConnectionListener {
_70
override fun onConnected(appRemote: SpotifyAppRemote) {
_70
spotifyAppRemote = appRemote
_70
Log.d("MainActivity", "Connected! Yay!")
_70
// Now you can start interacting with App Remote
_70
override fun onFailure(throwable: Throwable) {
_70
Log.e("MainActivity", throwable.message, throwable)
_70
// Something went wrong when attempting to connect! Handle errors here
_70
private fun connected() {
_70
spotifyAppRemote?.let {
_70
val playlistURI = "spotify:playlist:37i9dQZF1DX2sUQwD7tbmL"
_70
it.playerApi.play(playlistURI)
_70
// Subscribe to PlayerState
_70
it.playerApi.subscribeToPlayerState().setEventCallback {
_70
val track: Track = it.track
_70
Log.d("MainActivity", track.name + " by " + track.artist.name)
_70
override fun onStop() {
_70
spotifyAppRemote?.let {
_70
SpotifyAppRemote.disconnect(it)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4