Spotify is an international media services provider based in Sweden. It a streaming service that allows users to stream and listen to limitless music daily. Users can also save music offline for later listening when they are not connected to the Internet.
It was founded in 2006 but launched in 2008. Getting access to Spotify is relatively easy as users can just visit their site and log in with Facebook or register with an email. The app can also be downloaded from the Google Play Store. When a user is registered, they can listen to music for free with the standard subscription or premium with more options and features. There is no option however for downloading music files.
Understanding Android SDK
Android SDK stands for android Software Development Kit. It encompasses the tools needed to develop an Android application and some of these development tools are debuggers, libraries, a handset emulator based on QEMU, documentation, sample code, and tutorials. Although it had it’s initial release eleven years ago, it’s first stable release was 2+ years ago in 2017.
Users can go To the website to download the android studio and SDK tools for building mobile applications. If you are a developer looking to know about track beats level, energy, valence by measuring the characteristics of the track and do more in-debt analysis then the Spotify developer platform is right for you.
Intergrating Spotify
Below is a list of the steps to follow to integrate your android SDK and Spotify. At this stage, it is assumed that you have an android application ready that you want to integrate with Spotify.
- The first step is to create an account if you don’t have one and register your application here with Spotify Developer on the Spotify dashboard.
- For security reasons, fingerprints need to be registered along with the app. It is more secure to create two fingerprints, one for development and one for launching. Save the fingerprint and package name.
- Download and install the Spotify Android SDK. Also, go to google play store and download the Spotify app. Install it and log in with the email and password or register.
- Inside your android app code, go to the service you want to integrate with Spotify, if you do not have one create a service or activity. In the mainActivity code block, edit it with the code below.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
//code.
}
private void connected() {
// more code
}
@Override
protected void onStop() {
super.onStop();
//The end
}
}
- Now add the Spotify SDK into your project library by importing it as a module. Create a new module in Android Studio and under options import the .JAR/AAR Package and go to next.
- Click on the three dots and you will see the Spotify-app-remote-release-version.aar under the “app-remote-lib” folder in the unzipped bundle.
- Open the folder and give it a name. We’re using spotify-sdk in this example.
- Press Finish when you are done. This will import .arr into your project.
- Add dependencies into the subproject you just imported and Gson to your app’s build. Gradle file.
dependencies {
// app dependencies
implementation project(‘:spotify-sdk’)
implementation “com.google.code.gson:,gson:2.8.5”
}
- The next step is to authorize your application Spotify SDK, to do this, the user needs to authorize your application to do so. Below is how to enable users to allow authorize connection from your application. Use the built-in authorization flow by the default onStart method and add the following:
// Set the connection parameters
ConnectionParams connectionParams =
new ConnectionParams.Builder(CLIENT_ID)
.setRedirectUri(REDIRECT_URI)
.showAuthView(true)
.build();
- This connects your app to your Spotify account when the user logs in and authorizes access to your app. You can also add the following code to your onstart method.
SpotifyAppRemote.connect(this, connectionParams,
new Connector.ConnectionListener() {
@Override
public void onConnected(SpotifyAppRemote spotifyAppRemote) {
mSpotifyAppRemote = spotifyAppRemote;
Log.d(“MainActivity”, “Connected! Yay!”);
// Now you can start interacting with App Remote
connected();
}
@Override
public void onFailure(Throwable throwable) {
Log.e(“MainActivity”, throwable.getMessage(), throwable);
// Something went wrong when attempting to connect! Handle errors here
}
});
The andriod SDK app uses the same redirect URI, client ID, and scopes when connecting to Spotify. The code above uses the SpotifyAppRemote.Connector to connect to Spotify and get an instance of SpotifyAppRemote.
Did this help? Let us know!