How to implement Spotify


Badge

Hi everyone, I am a developer and Im building a .NET app in order to play music from Sonos ZP100 using Spotify. Or alternatively with music plugged into an USB. I tried using the UPnP protocol but it doesn seem to be the right way to reproduce songs. I am only able to play pause stop seek next previous and volume controls but nothing else. Do somebody know how to do something similar?
The perfect example is the SONOS S1 app that after linking spotify account can reproduce songs


This topic has been closed for further comments. You can use the search bar to find a similar topic, or create a new one by clicking Create Topic at the top of the page.

22 replies

Badge +17

Hi @Manatee19, welcome to the community.

 

Everything you’ve just described already exists on Sonos though one means or another. If you are looking to build your own controller app then I will point you in the direction of our Sonos Developer site that includes all the support and documention you need to integrate into the Sonos API. We also have a tag on Stack Overflow for any development questions.

 
 
 
 
 
Badge +20

I don’t know anything about about .NET but Python in conjunction with SoCo is very powerful.

Userlevel 7
Badge +23

Music playback is via UPnP, specifically AVTransport1.SetAVTransportURI. However the kicker is getting the correct url for a Spotify stream.

To do that you need to be a SMAPI client, and you can read about SMAPI at the link in the previous post.

However the real tricky bit is to get the user’s credentials from the Sonos system so you can authenticate with the Spotify SMAPI server. That requires you to decrypt the encrypted authentication data.

A few open source projects have avoided this problem by mixing the public Spotify API with UPnP, but this works with Spotify only (and maybe a couple of other services too). I am not aware of an open source project that includes the necessary decryption to work with every music service.

On the other hand playing a file from a USB stick is a lot more straight-forward. Just run a small web server and pass its url to SetAVTransportURI.

Badge

Music playback is via UPnP, specifically AVTransport1.SetAVTransportURI. However the kicker is getting the correct url for a Spotify stream.

To do that you need to be a SMAPI client, and you can read about SMAPI at the link in the previous post.

However the real tricky bit is to get the user’s credentials from the Sonos system so you can authenticate with the Spotify SMAPI server. That requires you to decrypt the encrypted authentication data.

A few open source projects have avoided this problem by mixing the public Spotify API with UPnP, but this works with Spotify only (and maybe a couple of other services too). I am not aware of an open source project that includes the necessary decryption to work with every music service.

On the other hand playing a file from a USB stick is a lot more straight-forward. Just run a small web server and pass its url to SetAVTransportURI.

Actually I just need spotify and nothing else and the process of getting the secret and key (if they will be needed) will be done initially and manually by a technician, but after that it’s all dark. Im not sure If I need to get these keys: direct-control-authorize in order to control it. Or I can control it maybe just getting the spotify keys and somehow getting the songs-URI

Badge

I don’t know anything about about .NET but Python in conjunction with SoCo is very powerful.

I will take a look at it

Badge

Hi @Manatee19, welcome to the community.

 

Everything you’ve just described already exists on Sonos though one means or another. If you are looking to build your own controller app then I will point you in the direction of our Sonos Developer site that includes all the support and documention you need to integrate into the Sonos API. We also have a tag on Stack Overflow for any development questions.

 
 
 
 
 

Actually I dont want to create a full Controller I just want to retrieve the playlist of the user and play a song, nothing else. What do you think I need to accomplish this?
Thank you

Userlevel 7
Badge +23

Hi @Manatee19, welcome to the community.

 

Everything you’ve just described already exists on Sonos though one means or another. If you are looking to build your own controller app then I will point you in the direction of our Sonos Developer site that includes all the support and documention you need to integrate into the Sonos API. We also have a tag on Stack Overflow for any development questions.

 

Actually I dont want to create a full Controller I just want to retrieve the playlist of the user and play a song, nothing else. What do you think I need to accomplish this?
Thank you

What kind of playlist exactly? If you want to use a Sonos Playlist, you can do that using the well documented Sonos Cloud API. If you want a Spotify playlist then I would use SMAPI, but you can’t as you don’t have the required secrets. I’ve never looked at the Spotify API, but I think others have figured out how to get the urls from that into a form that a Sonos speaker will accept. Good luck.

Badge

Hi @Manatee19, welcome to the community.

 

Everything you’ve just described already exists on Sonos though one means or another. If you are looking to build your own controller app then I will point you in the direction of our Sonos Developer site that includes all the support and documention you need to integrate into the Sonos API. We also have a tag on Stack Overflow for any development questions.

 

Actually I dont want to create a full Controller I just want to retrieve the playlist of the user and play a song, nothing else. What do you think I need to accomplish this?
Thank you

What kind of playlist exactly? If you want to use a Sonos Playlist, you can do that using the well documented Sonos Cloud API. If you want a Spotify playlist then I would use SMAPI, but you can’t as you don’t have the required secrets. I’ve never looked at the Spotify API, but I think others have figured out how to get the urls from that into a form that a Sonos speaker will accept. Good luck.

The spotify playlist of the account linked using the sonos S1 App. Anyway I was able to get the token to send request using the API but when I tried to ask for households it return an empty list. Maybe is the ZP100 too old?

Userlevel 7
Badge +23

I am confused. What are you asking for Households exactly? Spotify??

Badge

I am confused. What are you asking for Households exactly? Spotify??

Well I think I am. I was calling some random stuff in order to see if I will receive something and I understood that household is like the device.

My final goal is to be able to get the Spotify play lists and reproduce songs from them on my sonos device. Until now I was only able to get the auth token and now I can make all API calls, but I don't know which one can help me.

Badge

I am confused. What are you asking for Households exactly? Spotify??

I was able to finally call the favorites and the sonos playlist, now im looking on how to receive playlist from spotify and play a specific song in the playlist

Spotify app on any phone to target Sonos speaker. Job done.

Badge

Spotify app on any phone to target Sonos speaker. Job done.

Eh eh eh, I will like it if it was in that way but we are developing an app that includes all the service between those there is sonos with playback of spotify songs…. Do youhave any other idea?

Badge

I am confused. What are you asking for Households exactly? Spotify??

I want to do the same stuff that SONOS S1 app does, connect the spotify service somehow and play audio on sonos. There is a way using API (reading on stack it seems that there isnt a way to do this), if not what should I use to do this? UPnP, using loadAudioClip and grab in some way a streamUrl from Spotify?
Super thank you boss

Userlevel 7
Badge +23

I am confused. What are you asking for Households exactly? Spotify??

I want to do the same stuff that SONOS S1 app does, connect the spotify service somehow and play audio on sonos. There is a way using API (reading on stack it seems that there isnt a way to do this), if not what should I use to do this? UPnP, using loadAudioClip and grab in some way a streamUrl from Spotify?
Super thank you boss

  1. Obtain key and token for the user’s Spotify account from the Sonos system (very hard)
  2. Use SMAPI to connect to Spotify and enumerate the playlists (easy if you know REST)
  3. Pick one and take the url and pass it to AVTransport1.SetNextAVTransport (UPNP call to speaker) (trivial if you know UPnP)

No idea if you could replace #3 with loadAudioClip

Userlevel 7
Badge +20

Hi everyone, I am a developer and Im building a .NET app in order to play music from Sonos ZP100 using Spotify. Or alternatively with music plugged into an USB. I tried using the UPnP protocol but it doesn seem to be the right way to reproduce songs. I am only able to play pause stop seek next previous and volume controls but nothing else. Do somebody know how to do something similar?
The perfect example is the SONOS S1 app that after linking spotify account can reproduce songs

Just to note that playing local files (e.g., from USB drives) on Sonos is one of the features offered by SoCo-CLI. It works just as @controlav describes, but is implemented in Python:

https://github.com/avantrec/soco-cli#audio-files-on-the-local-filesystem

Also, we’re currently working on music service (re)enablement in SoCo. It’s tricky, but once it’s available, I’ll add it to SoCo-CLI.

https://github.com/SoCo/SoCo/pull/763

Badge

Hi everyone, I am a developer and Im building a .NET app in order to play music from Sonos ZP100 using Spotify. Or alternatively with music plugged into an USB. I tried using the UPnP protocol but it doesn seem to be the right way to reproduce songs. I am only able to play pause stop seek next previous and volume controls but nothing else. Do somebody know how to do something similar?
The perfect example is the SONOS S1 app that after linking spotify account can reproduce songs

Just to note that playing local files (e.g., from USB drives) on Sonos is one of the features offered by SoCo-CLI. It works just as @controlav describes, but is implemented in Python:

https://github.com/avantrec/soco-cli#audio-files-on-the-local-filesystem

Also, we’re currently working on music service (re)enablement in SoCo. It’s tricky, but once it’s available, I’ll add it to SoCo-CLI.

https://github.com/SoCo/SoCo/pull/763

Im bonded to C# I cant use python unfortunately

Badge

I am confused. What are you asking for Households exactly? Spotify??

I want to do the same stuff that SONOS S1 app does, connect the spotify service somehow and play audio on sonos. There is a way using API (reading on stack it seems that there isnt a way to do this), if not what should I use to do this? UPnP, using loadAudioClip and grab in some way a streamUrl from Spotify?
Super thank you boss

  1. Obtain key and token for the user’s Spotify account from the Sonos system (very hard)
  2. Use SMAPI to connect to Spotify and enumerate the playlists (easy if you know REST)
  3. Pick one and take the url and pass it to AVTransport1.SetNextAVTransport (UPNP call to speaker) (trivial if you know UPnP)

No idea if you could replace #3 with loadAudioClip

  1. I have access to user’s spotify account (in order to create key and secret) so I guess the point 1 can be skipped?
  1. I read the SMAPI doc here: SMAPI but there isnt a call that retrieve an URL except for the getMediaURI that seems to return an URI to the stream, but there isnt the Endpoint indicated in the SMAPI.
  2. I dont know UPnP and I was searching for some lib for C# but I dont find nothing easily to use. Can you give me an example of URL to pass to the SetNextAVTransport?

    If can help I have full access to SONOS and SPOTIFY user’s account
    Thank you, you are kind and helpful.
Userlevel 7
Badge +23
  1. I don’t think you can take the key and secret from the Spotify and pass them to SMAPI, but I never tried it, so worth a shot
  2. getMetadata is the call you need, starting at “root” and navigate through that so you can get to the user’s playlists. Once you have done that once, you will probably find a fixed id for users playlists and can go straight to it.
  3. There are lots of UPnP APIs for C# around (I have not used any of them), and if on Windows you can use the built-in OS support for it (which I last used about a decade ago).

Actually i misspoke: for a Spotify playlist you will need to AddURIToQueue not SetNextAVTransport. The URI would be something like "x-rincon-cpcontainer:0006008cspotify%3aplaylist%3a3irbM1vEqph2KusCP" and the metadata would be a basic DIDL-lite structure containing an id similar to the url (but different). All of this is trivial to see by taking a network capture of the Sonos app starting a Spotify playlist.

Badge
  1. If I cant pass data to the SMAPI how can I use this SMAPI? I look for an endpoint of the SMAPI and I didnt find it. The unique sneak I found is this:

    but this page is no longer accessible. If I understand correctly the SMAPI server is something that I need to create and host and then this server redirect the REST call to the music service using their API, is it correct? That’s why there isnt an endpoint on SMAPI DOC?

  2. Ok

  3. I tried some but no documentation at all so it’s tricky

  4. You talk about capture a frame form the sonos app, how can I accomplish this, I was thinking about wireshark but my pc is wired so Im not sure I can get the frame that start from my phone and I guess they are encrypted.
    Thank you

Userlevel 7
Badge +23
  1.  
  2. You talk about capture a frame form the sonos app, how can I accomplish this, I was thinking about wireshark but my pc is wired so Im not sure I can get the frame that start from my phone and I guess they are encrypted.
    Thank you

The UPnP calls are not encrypted. I use NetMon to capture them, I know others use Wireshark. Capture PC traffic, its a lot easier than capturing phone traffic in my experience. Wired or wireless makes no difference - its still network traffic. It is possible to capture phone traffic but its harder: I use additional network hardware and a BOOST when I need phone captures.

If you can’t capture anything, give up now, its an essential part of the reverse engineering toolkit.

Badge
  1.  
  2. You talk about capture a frame form the sonos app, how can I accomplish this, I was thinking about wireshark but my pc is wired so Im not sure I can get the frame that start from my phone and I guess they are encrypted.
    Thank you

The UPnP calls are not encrypted. I use NetMon to capture them, I know others use Wireshark. Capture PC traffic, its a lot easier than capturing phone traffic in my experience. Wired or wireless makes no difference - its still network traffic. It is possible to capture phone traffic but its harder: I use additional network hardware and a BOOST when I need phone captures.

If you can’t capture anything, give up now, its an essential part of the reverse engineering toolkit.

If the UPnP are not encrypted should be “easy”.

I dunno what is a BOOST but probably I will use the SONOS DESKTOP APP so I can grab the traffic and see it without problem in order to understand what kind of call it does.
 

Anyway I thought I reach the solution: after experimenting with Spotify’s API I found that there is a list of devices used to reproduce songs. I grab the list, make another call with device id and playlist and it play. The problem has been that the SONOS devices wasnt in that list…. All my device (alexa, google, my phone, my computer) there were except for sonos… do you have a clue on why this happens?

I will try using the method you said: capture the network traffic and see, then reproduce with code on C# using some custom upnp sender.

Thank you