How To Register As A Spotify Developer
This tutorial leads you step-past-step through the creation of a uncomplicated app that uses the Android SDK to play a playlist and subscribe to histrion state updates.
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 information
If y'all are new to developing Android apps, we recommend reading the tutorials on Google's Android developer portal.
Yous tin read more about the Android SDK in the overview, or dig into the reference documentation.
Set up Your Environment
Annals Your App
Yous volition need to annals your application on the Programmer Dashboard and obtain a client ID. When you register your app y'all will also need to whitelist a redirect URI that the Spotify Accounts Service volition utilize to callback to your app after authorization. You besides should add together your package name and app fingerprint as they're used to verify the identity of your application.
Annals App Fingerprints
Fingerprints are used for authentication betwixt your Android Application and the Spotify service. You'll demand to generate a fingerprint for your app and register it in your Dashboard. We strongly recommend that you create both a development and a product fingerprint for security reasons.
To Create a Development Fingerprint
-
Run the following control in your Terminal and add a countersign when required:
# On Bash mode shells $ keytool -alias androiddebugkey -keystore ~/.android/debug.keystore -list -five | grep SHA1 # On Windows Powershell $ keytool -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore -list -v | grep SHA1Y'all should await to receive a fingerprint that looks like this:
SHA1: E7:47:B5:45:71:A9:B4:47:EA:Advertizement:21:D7:7C:A2:8D:B4:89:1C:BF:75 -
Re-create the fingerprint and your parcel name and enter it in the Spotify Developer Dashboard, under the "Edit settings" department. Don't forget to click Save later on you added the fingerprints in the dashboard.
To Create a Release Fingerprint
The development and production versions of your application usually have different certificates for security reasons.
-
Run the post-obit command in your Terminal (no password):
# On Bash style shells $ keytool -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEYSTORE_PATH> -list -v | grep SHA1 # On Windows Powershell $ keytool -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEYSTORE_PATH> -list -v | grep SHA1You should expect to receive a fingerprint that looks like this:
SHA1: E7:47:B5:45:71:A9:B4:47:EA:Advertizement:21:D7:7C:A2:8D:B4:89:1C:BF:75 -
Copy the fingerprint and your parcel proper name and enter information technology in the Spotify Developer Dashboard. Don't forget to click Save after yous added the fingerprints in the dashboard.
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 yous desire to utilise for development. Run the Spotify app and login or sign upward.
Download the SDK
Download the Spotify Android SDK from our GitHub.
Create Your App
Create or make sure you lot have an Android app with at least i Activeness or Service in which y'all tin put your code to connect to Spotify.
Edit your MainActivity to look like this:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate ( Packet savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); } @Override protected void onStart () { super . onStart (); // We will offset writing our code here. } private void continued () { // And so nosotros will write some more lawmaking here. } @Override protected void onStop () { super . onStop (); // Aaand nosotros will finish off hither. } } Add together the App Remote SDK
Unzip the App Remote SDK nothing file that yous downloaded. Add together the library to your project by importing it as a module. In the "Projection" side bar in Android Studio (View –> Tool Windows –> Projection), correct 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 "…" push and locate the spotify-app-remote-release-version.aar under the "app-remote-lib" folder in the unzipped bundle. Click "Open up" and cull a suitable subproject name. Nosotros're using spotify-app-remote in this example. Click "Finish" to import the .aar into your project.
Tip: when updating the App Remote SDK with futurity updates of the SDK, merely supplant the .aar file in your project'due south directory.
Add together 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 of import that the proper noun that y'all specify in the build.gradle file is the same every bit the subproject name you lot decided on.
dependencies { // your app dependencies implementation project ( ':spotify-app-remote' ) implementation "com.google.code.gson:gson:2.8.five" } 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 run across the FAQ hither.
Import Dependencies
import com.spotify.android.appremote.api.ConnectionParams ; import com.spotify.android.appremote.api.Connector ; import com.spotify.android.appremote.api.SpotifyAppRemote ; import com.spotify.protocol.customer.Subscription ; import com.spotify.protocol.types.PlayerState ; import com.spotify.protocol.types.Rails ; Ready your Spotify credentials
private static final String CLIENT_ID = "your_client_id" ; private static final Cord REDIRECT_URI = "http://com.yourdomain.yourapp/callback" ; private SpotifyAppRemote mSpotifyAppRemote ; To be able to use the App Remote SDK, the user needs to qualify your application to exercise so. If they oasis't, the connection will neglect withUserNotAuthorizedException. To allow the user to authorize your app, you will utilise the built-in authorization described below.
The built-in authorization flow is an authorization method designed to make things easier for y'all. It simply requires you to pass the respective parameters that you used when your application was created. Then, the Spotify client volition try to authorize your app's session and will utilize the ConnectionListener callback to send the authority result.
For this utilize-case, you don't take to add together the Say-so Library to your application. You can asking that the authorization view is shown to users who have not approved the app-remote-control telescopic by passing the flag showAuthView in the ConnectionParams. The scope is automatically requested for you by the library.
Add together the post-obit to your onStart method:
// Ready the connection parameters ConnectionParams connectionParams = new ConnectionParams . Builder ( CLIENT_ID ) . setRedirectUri ( REDIRECT_URI ) . showAuthView ( true ) . build (); Built-in auth provides offline back up. This ways that a user can exist authorized fifty-fifty if the device is currently offline. Offline back up 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
Connect to App Remote
The first matter we demand to exercise is to use the SpotifyAppRemote.Connector to connect to Spotify and get an instance of SpotifyAppRemote. To do this, nosotros phone call the SpotifyAppRemote.connect method using the connectionParams we defined above. Add the following to your onStart method.
SpotifyAppRemote . connect ( this , connectionParams , new Connector . ConnectionListener () { @Override public void onConnected ( SpotifyAppRemote spotifyAppRemote ) { mSpotifyAppRemote = spotifyAppRemote ; Log . d ( "MainActivity" , "Connected! Yay!" ); // At present you can start interacting with App Remote continued (); } @Override public void onFailure ( Throwable throwable ) { Log . e ( "MainActivity" , throwable . getMessage (), throwable ); // 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 control. From there nosotros tin can become the PlayerApi directly and call play. Add the following to your private continued method:
// Play a playlist mSpotifyAppRemote . getPlayerApi (). play ( "spotify:playlist:37i9dQZF1DX2sUQwD7tbmL" ); Run the application and y'all should hear some experience-good indie tunes playing on your phone. If yous prefer something more than relaxing, why not attempt some sweetness pianoforte musicspotify:playlist:37i9dQZF1DX7K31D69s4M1. If you don't hear music playing and end upwardly in the onFailure callback to a higher place, please read up on connection errors and endeavour once again.
Subscribe to PlayerState
The PlayerApi offers, in add-on to the play(uri) method we use above, the ability to subscribe to and poll for the country of the Spotify thespian. Add the following to your lawmaking to subscribe to PlayerState and log the track title and artist of the song that will be playing:
// Subscribe to PlayerState mSpotifyAppRemote . getPlayerApi () . subscribeToPlayerState () . setEventCallback ( playerState -> { final Rail track = playerState . track ; if ( rail != null ) { Log . d ( "MainActivity" , track . proper noun + " past " + runway . artist . proper name ); } }); Run the app again. Yous should at present see a log with the runway 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.
@Override protected void onStop () { super . onStop (); SpotifyAppRemote . disconnect ( mSpotifyAppRemote ); } Next Steps
Congratulations! You've interacted with the App Remote SDK for the first time. Time to celebrate, yous did a great task! 👏
Desire more? Here'southward what y'all can do next:
- Larn about best practices for handling App Remote SDK interactions and connections in your Android application in our guides.
- Dive into other things you can practice with the SDK in the App Remote SDK Reference.
Source Code
The Quick Start source code is below. Re-create into your Main Activity.
parcel com.yourdomain.yourapp ; import android.bone.Package ; import android.back up.v7.app.AppCompatActivity ; import android.util.Log ; import com.spotify.android.appremote.api.ConnectionParams ; import com.spotify.android.appremote.api.Connector ; import com.spotify.android.appremote.api.SpotifyAppRemote ; import com.spotify.protocol.client.Subscription ; import com.spotify.protocol.types.PlayerState ; import com.spotify.protocol.types.Track ; public class MainActivity extends AppCompatActivity { private static concluding String CLIENT_ID = "your_client_id" ; private static final String REDIRECT_URI = "com.yourdomain.yourapp://callback" ; private SpotifyAppRemote mSpotifyAppRemote ; protected void onCreate ( Package savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); } @Override protected void onStart () { super . onStart (); ConnectionParams connectionParams = new ConnectionParams . Architect ( CLIENT_ID ) . setRedirectUri ( REDIRECT_URI ) . showAuthView ( true ) . build (); SpotifyAppRemote . connect ( this , connectionParams , new Connector . ConnectionListener () { public void onConnected ( SpotifyAppRemote spotifyAppRemote ) { mSpotifyAppRemote = spotifyAppRemote ; Log . d ( "MainActivity" , "Connected! Yay!" ); // At present yous tin start interacting with App Remote continued (); } public void onFailure ( Throwable throwable ) { Log . e ( "MyActivity" , throwable . getMessage (), throwable ); // Something went wrong when attempting to connect! Handle errors hither } }); } @Override protected void onStop () { super . onStop (); SpotifyAppRemote . disconnect ( mSpotifyAppRemote ); } private void connected () { // Play a playlist mSpotifyAppRemote . getPlayerApi (). play ( "spotify:playlist:37i9dQZF1DX2sUQwD7tbmL" ); // Subscribe to PlayerState mSpotifyAppRemote . getPlayerApi () . subscribeToPlayerState () . setEventCallback ( playerState -> { concluding Track track = playerState . track ; if ( track != zip ) { Log . d ( "MainActivity" , track . name + " by " + rails . artist . name ); } }); } } How To Register As A Spotify Developer,
Source: https://developer.spotify.com/documentation/android/quick-start/
Posted by: rileyaccar1986.blogspot.com

0 Response to "How To Register As A Spotify Developer"
Post a Comment