How to automatically play playlist in Spotify Android app

I am trying to play a famous playlist in the Spotify application. The best I have is to download a playlist, but not play.

Two things I've tried. Play from search first:

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 tried replacing PLAYLIST with the name of a famous playlist. Also tried things like "4Rj0zQ0Ux47upeqVSIuBx9", "spotify: user: 11158272501: playlist: 4Rj0zQ0Ux47upeqVSIuBx9", etc. All this makes an unsuccessful search for these strings.

The second attempt is an intention of the form:

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

Loads a playlist, but does not play. If I then use one of the methods to send the key KEYCODE_MEDIA_PLAY, it just resumes the current playlist, and not this recently downloaded list.

Any help anyone (including Spotify devs)?

BTW I do not want to use the Spotify SDK to implement my own Spotify Player - it seems shameful to do this when a completely good player is already installed on the user device.

+6
source share
1 answer

I found the answer to this blog . What I was missing was the playlist URI as the Uri data in the intent. Therefore, do not use the correct Android method to search from a search.

So, using the first method from the question, you get the following:

 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 notes:

  • Uri cannot be https: //play.spotify ... but must be a colon separated.
  • Spotify account must be premium, otherwise the playlist opens, but does not play.
  • this does not work if the screen is off or the lock screen is displayed. The spotify operation must be displayed on the screen before downloading and playing!

This last point means that it is not really being used for me ... so for now we are not accepting the answer.

+7
source

Source: https://habr.com/ru/post/983817/


All Articles