Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a Song from Spotify Intent

Is there anyway to start a Spotify Track from its URI ?

I've tried the following approaches but none of them work. When Spotify opens, it always lands in the Playlists page, instead of the track's player.

String spotifyTrackURI = "spotify:track:1cC9YJ8sQjC0T5H1fXMUT2";
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.spotify.mobile.android.ui");

// I've tried with Intent#putExtra()..
launchIntent.putExtra( SearchManager.QUERY, spotifyTrackURI );
// or with setData
launchIntent.setData(Uri.parse(spotifyTrackURI))

context.startActivity(launchIntent);

Thanks

like image 932
dornad Avatar asked Oct 02 '12 21:10

dornad


2 Answers

Found the answer on the #spotify channel on Freenode (irc). Thanks everyone!

I'm putting it here for other people to know about:

// right click on a track in Spotify to get the URI, or use the Web API.
String uri = "spotify:track:<spotify uri>"; 
Intent launcher = new Intent( Intent.ACTION_VIEW, Uri.parse(uri) );
startActivity(launcher);
like image 189
dornad Avatar answered Nov 02 '22 12:11

dornad


First one is for old Spotify version. For new versions you can use the second.

In this way it will first try with old version, if it fails with an exception it try with the second one :)

try {
                final 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, artistName + " " + trackName );
                context.startActivity(intent);
            } catch ( ActivityNotFoundException e ) {
                final 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.activity.MainActivity"));
                intent.putExtra(SearchManager.QUERY, artistName + " " + trackName );
                context.startActivity(intent);
            }
like image 24
StErMi Avatar answered Nov 02 '22 10:11

StErMi