How to trigger Spotify playback with a song name from an Android application?

Hey everyone! I’m working on an Android app and I’m trying to figure out how to make it play songs on Spotify. What I want to do is send a song title from my app to Spotify and have it start playing right away. I know Shazam can do this, so it must be possible, right?

I found some code online that I thought might work, but when I tried it, nothing happened. Here’s what I used:

Intent spotifyIntent = new Intent(Intent.ACTION_MAIN);
spotifyIntent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
spotifyIntent.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity"));
spotifyIntent.putExtra(SearchManager.QUERY, "Queen Bohemian Rhapsody");

Does anyone know if there’s a way to make this work? Or is there a better method to get Spotify to play a specific song from my app? Any help would be awesome!

hey sofiag, i’ve messed with this before. spotify’s api is ur best bet for what u want. you’ll need to register ur app and get api keys. then use their android sdk to control playback. it’s a bit of work but way more reliable than tryin to use intents. good luck!