Send the name of the song to determine to start playing with the Android application

Is there a way to send the song title to the spotify application from my application so that it starts playing the song through spotify?

I tried using the following code, which 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 that Shazam is able to do this.

+5
source share
1 answer

You only create an intention, but you do not start an intention.

add this line after setting your intention

startActivity(intent);

So the full code would look like this:

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);
}
+7
source

All Articles