Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send song title to spotify to start playing from android app

Tags:

android

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.

like image 834
Peter Avatar asked Dec 12 '11 02:12

Peter


1 Answers

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);
}
like image 126
poitroae Avatar answered Nov 10 '22 07:11

poitroae