Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically play Playlist in Spotify Android app

I'm trying to play a known playlist in the Spotify app. The best I've got is to load the playlist, but not play it.

Two things I've tried. Firstly to play from search:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.setComponent(new ComponentName("com.spotify.music",
    "com.spotify.music.MainActivity"));
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, 
    MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE);
intent.putExtra(MediaStore.EXTRA_MEDIA_PLAYLIST, <PLAYLIST>);
intent.putExtra(SearchManager.QUERY, <PLAYLIST>);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

I've tried replacing PLAYLIST with the name of a known playlist. Also tried things like "4Rj0zQ0Ux47upeqVSIuBx9", "spotify:user:11158272501:playlist:4Rj0zQ0Ux47upeqVSIuBx9" etc. All these do is a failed search for these strings.

Second attempt is the View intent:

String uri = "https://play.spotify.com/user/11158272501/playlist/4Rj0zQ0Ux47upeqVSIuBx9";
Intent intent= new Intent( Intent.ACTION_VIEW, Uri.parse(uri) );
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

This loads the playlist, but does not play. If I then use one of the many ways to send a KEYCODE_MEDIA_PLAY key, it just resumes the currently playing list, not this newly loaded list.

Any help from anyone (including Spotify devs)?

BTW I don't want to use the Spotify SDK to implement my own Spotify Player - it seems a shame to have to do this when a perfectly good player is already installed on a user's device.

like image 615
barkside Avatar asked Mar 15 '15 10:03

barkside


1 Answers

I found the answer from this blog. What I was missing was the playlist URI as a data Uri in the intent. So not using the proper Android way of Play from search.

So, using the first method from the question, you end up with this:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.setData(Uri.parse(
    "spotify:user:11158272501:playlist:4Rj0zQ0Ux47upeqVSIuBx9"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

3 things to note:

  • the Uri can't be the https://play.spotify... but has to be the colon seperated type.
  • the Spotify account must be premium, otherwise the playlist opens but does not play.
  • this does not work if the screen is off, or lock screen is showing. The spotify activity needs to display on the screen before it loads and plays!

This final point means it isn't actually usable for me... so not accepting answer yet.

like image 95
barkside Avatar answered Oct 02 '22 05:10

barkside