Spotify Shuffle toggle... anyone has a solution?

Hi there smart guys…

I’m trying to achieve a “simple” implementation having a Sharptools.io Consolle able to activate different playlists on My Echo Speakers… for this reason I added the Echo Speaks smart app which allows me using the SearchMusic methods to activate my wanted playlist… which works… BUT…

  1. if I’m using Amazon Music playlists i’m able to activate the shuffle function randomly using the String parameter “shuffle xxx playlist”… this for some strange reasons works only with some playlist and lot with others…

  2. if I’m using Spotify which I would prefer there is no way to add the shuffle command in the same string as the search 'cause it invalidates the request and then the playlist would not play…

Browsing the forums and the wed I found out this method from Spotify: https://api.spotify.com/v1/me/player/shuffle (documentation here:https://developer.spotify.com/console/put-shuffle/?state=true&device_id=) using the simulator I’m able to create and invoke a curl which enable the shuffle on Spotify.

In this way, if I add a Virtual switch with this capability on Smartthings, and I activate it from Sharptools just after starting my playlist I would be able to shuffle the playlist, at least from the second song on… does that make sense?

My issue is: how can I call a cURL from smartthing device handler? I was not able to find a already available app or handler doing that.
My second issue: in the web interface above I need to get a OAuth token every time I make the call… how could i do the same from the handler?

Thanks all who’d like to help

2 Likes

Hey @dv73 - glad to hear you are enjoying SharpTools.io!

I’m tagging my colleague @jamesguitar3 who uses Echo Speaks in a Jukebox style dashboard to see if he has any thoughts on the first item. Are there specific types of playlists that it seems to work on and specific types that don’t? In the meantime, have you posted in the Echo Speaks thread to see if anyone else has experienced anything similar with the shuffle syntax?

For item 2, that’s definitely an interesting approach! Are you trying to shuffle the Spotify playlists on an Amazon Echo device? If so, I’d be curious if anyone in the Echo Speaks thread might have run into the same thing.

Edit: Here’s a screenshot of one of James’ jukebox dashboards:


Custom Device Driver Thoughts

Ideally, if this could be figured out directly in the Echo Speaks driver, that would probably keep things much simpler. That being said, if you’re interested in going down the path of a custom device handler, here’s some thoughts…

To the question about running a cURL command from SmartThings, since curl is really just sending HTTP requests under the hood, you can use the standard SmartThings httpGet, httpPost, httpPostJson methods.

OAuth tends to be a bit trickier, but the same http requests can help you with swapping an OAuth Refresh Token for a Access Token. You’ll likely want to schedule some sort of periodic call to swap the refresh token for an access token. SmartThings provides several runEveryX methods and a runIn method that might be helpful for the scheduling side of things. Step 4 of Spotify’s Authorization Guide seems to have this documented:

4. Requesting a refreshed access token; Spotify returns a new access token to your app

Access tokens are deliberately set to expire after a short time, after which new tokens may be granted by supplying the refresh token originally obtained during the authorization code exchange.

The request is sent to the token endpoint of the Spotify Accounts service:

POST https://accounts.spotify.com/api/token

The body of this POST request must contain the following parameters encoded in application/x-www-form-urlencoded as defined in the OAuth 2.0 specification:

REQUEST BODY PARAMETER VALUE
grant_type Required. Set it to refresh_token .
refresh_token Required. The refresh token returned from the authorization code exchange.

The header of this POST request must contain the following parameter:

HEADER PARAMETER VALUE
Authorization Required. Base 64 encoded string that contains the client ID and client secret key. The field must have the format: Authorization: Basic <base64 encoded client_id:client_secret>
1 Like

Thanks Josh for the prompt reply, very useful information here, I’ll have a look at it.

Yea, some playlists seems to work while others do not, possibly it the passing of the sentences in the Amazon Music interpreter that depending on the playlist names identify or not the shuffle request.

In the methods list for Echo Speaks I did not find anything for shuffle but I did not go through the code. I will also post there to get more people to understand the issue.

And yes, calling the curl directly on Spotify it triggers the shuffle mode while playing so that would work.

I’ll explore some more options in the direction you pointed me.

In the while I figured out that it’s possible to achieve a similar result using the Amazon Music Radios which shuffle songs automatically as a Radio does. But I. This case you can’t use the playlists, it works well for ambient music.

Thanks

2 Likes

Hi @dv73 , can you try change the search String parameter to “Shuffle my James Favorites playlist”, instead of “Shuffle James Favorites playlist”, where “James Favorite” is the playlist name. (Or try changing my to the.)

I tested several playlists and had the same issues as you described, especially for those playlists I created. By following @MinerJason suggestion in his post, I was able to find the voice command to shuffle my list, and used it as the search parameter, and echo was able to take the Shuffle as the command and worked well then. Thanks for the cool idea, as I didn’t know the shuffle trick before. :sunglasses:

2 Likes

For getting the shuffle command to work with Echo Speaks and Amazon Music I’ve found that specifying “shuffle my xxx playlist” helps a lot. It also forces it to look in your personal playlists and avoids selecting a public playlist that might have a similar name to one of your personal playlists.

Edit: @jamesguitar3 beat me to it…

2 Likes

Does Echo Speaks have a separate command for just running a voice command?

As in, could you first run your search string with “shuffle my awesome Spotify playlist”… then run a separate “voice” command to ask for the shuffle?

No, the music commands are: searchAmazonMusic(searchPhrase) or searchSpotify(searchPhrase), etc, where the searchPhrase is the song, artist, or playlist you want to play.

Trial and error testing indicated that some additional commands could be included in the search phrase, but only those that could be replicated by a single voice command, which for the most part seems to be limited to using the Amazon music service. I have been able to add speaker group commands as well (“shuffle my random playlist on the everywhere group”), but that’s not really helpful since you can define which echo devices or speaker groups you want to use in other ways that are much more flexible.

If you have other Echo devices or other TTS devices within earshot of the Echo you’re playing music on you could always use the really hacky method where the TTS “Shuffle” command is spoken from a different device.

1 Like

Tried some of your suggestions above (sorry I was off for a week)… what I did figure out is the following:

  • too tricky to “find the right words for the search command”, sometimes “shuffle MY playlist” works but most not and it seems to depend totally on the playlist you are calling… plut it does work ONLy with AMAZON MUSIC which is my second choice. So I’m giving the Spotify API a try.
    Also I did find some interesting APIs from spotify to letting you to select the speaker which might be useful in a “follow me” music scenario.

So I’ll do some more search on this option.

1 Like