Peter
Peter

Reputation: 5131

Send song title to spotify to start playing from android app

Is there any way to send a song title to the spotify app from my app so that it will start playing the song through spotify?

I tried using the bellow code i found in another code but nothing happens.

Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
                intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));
                intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal");

I know shazam is able to do this.

Upvotes: 5

Views: 2470

Answers (1)

poitroae
poitroae

Reputation: 21377

You are just creating an Intent, but you don't start the Intent.

add this line after setting up your intent

startActivity(intent);

So the complete code would look like that

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));
intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal");
try {
  startActivity(intent);
}catch (ActivityNotFoundException e) {
  Toast.makeText(context, "You must first install Spotify", Toast.LENGTH_LONG).show();  
  Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=com.spotify.mobile.android.ui"));
  startActivity(i);
}

Upvotes: 8

Related Questions