How to trigger Spotify to play specific track using Android intents?

I want to build functionality similar to what music recognition apps do when they open Spotify and start playing a detected song. I’ve been analyzing how this works and found some clues in the system logs.

When I check the logcat output, I can see that these apps use something like this:

Intent playIntent = new Intent();
playIntent.setAction("android.media.action.MEDIA_PLAY_FROM_SEARCH");
playIntent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));

This code successfully launches Spotify, but I’m stuck on the next step. How do I pass the actual song information so Spotify knows what track to search for and play? I tried using basic spotify URIs like spotify:track:trackid but that approach isn’t working as expected.

What extras or parameters should I add to the intent to specify which song should be played? Any help with the complete implementation would be great.

you might add the search query as an extra: playIntent.putExtra(SearchManager.QUERY, "artist - song title"); it worked for me earlier. Once you send the query, spotify takes care of the rest.

Honestly, the easiest way I’ve found is using the Spotify Web API intent. Try playIntent.putExtra("referrer", "your_app_package_name"); and playIntent.putExtra("track", "trackid"); - this bypasses some search issues. Works even without premium sometimes.

The intent structure looks right, but I’ve found a more reliable approach. Skip the media focus extras and add the Spotify URI directly with playIntent.putExtra("uri", "spotify:track:your_track_id"); alongside your search query. This combo works better - Spotify falls back to the URI if search fails, or uses the search query if the URI isn’t accessible. Don’t forget to catch ActivityNotFoundException for when Spotify isn’t installed. This only works with Spotify Premium though - free users just get the app opened without autoplay. I’ve tested across different Android versions and results vary, so wrap it in try-catch with backup search methods for production.

Had this exact problem last year on a similar project. You need MediaStore.EXTRA_MEDIA_FOCUS with your search query. Try playIntent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE); then add your search string with playIntent.putExtra(SearchManager.QUERY, \"your search term\");. The media focus extra tells Spotify what you’re searching for. Also make sure you’re using android.media.action.MEDIA_PLAY_FROM_SEARCH instead of the generic play action - works way better for this. Different Spotify versions handle intents slightly differently, so add some fallback logic just in case.

Been working with Spotify intents for a while and found something most developers miss. Combine your search query with proper intent flags. After setting up your base intent with MEDIA_PLAY_FROM_SEARCH, add playIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and playIntent.putExtra("query", "track:your_track_name artist:your_artist_name");. The track/artist prefixes make Spotify’s search way more precise. Newer Spotify versions actually respond better to android.intent.action.VIEW with a spotify URI as the data instead of extras. Test both approaches - Spotify’s intent handling changes constantly with updates. Handle cases where multiple results match your query since Spotify won’t always pick the exact track you want.