Starting a Song from Spotify Intent

痴心易碎 提交于 2019-12-02 19:36:50

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);

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

If you get playlist for Artist on Spotify app. You need to start following intent. You do not need to package name or activity of Spotify.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("spotify:artist:" + "ARTIST SPOTIFY LINK"));
                startActivity(intent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!