bencevans / node-sonos

🔈 Sonos Media Player Interface/Client
https://www.npmjs.com/package/sonos
MIT License
704 stars 147 forks source link

Trying to play a Spotify song throw 500 exception #393

Closed jeantristan closed 5 years ago

jeantristan commented 5 years ago

Hello,

For the record I have the same issue with sonos-cli (https://github.com/bencevans/sonos-cli/issues/20)

Expected Behavior

I want to be able to play a spotify song based on URI as mentioned in the documentation.

Current Behavior

I have a 500 exception raised when trying to play a spotify song based on URI

Possible Solution

I can't manage to find a workaround.

Sample code or executed example

When i try to execute the following code:

const { Sonos } = require('sonos')

const device = new Sonos('192.168.1.16');

device.play()
  .then(() => console.log('now playing'))

device.getVolume()
  .then((volume) => console.log(`current volume = ${volume}`))

var spotifyUri = 'spotify:album:1TSZDcvlPtAnekTaItI3qO'

device.play(spotifyUri).then(success => {
  console.log('Yeay')
}).catch(err => { console.log('Error occurred %j', err) })

I have the following result:

mbpt:playmusic-js chanegue$ node test.js 
current volume = 44
now playing
Error occurred {"name":"StatusCodeError","statusCode":500,"message":"500 - \"<s:Envelope xmlns:s=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\" s:encodingStyle=\\\"http://schemas.xmlsoap.org/soap/encoding/\\\"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns=\\\"urn:schemas-upnp-org:control-1-0\\\"><errorCode>800</errorCode></UPnPError></detail></s:Fault></s:Body></s:Envelope>\"","error":"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>800</errorCode></UPnPError></detail></s:Fault></s:Body></s:Envelope>","options":{"uri":"http://192.168.1.16:1400/MediaRenderer/AVTransport/Control","method":"POST","headers":{"SOAPAction":"\"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"","Content-type":"text/xml; charset=utf8"},"body":"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>x-rincon-cpcontainer:0004206cspotify%3aalbum%3a1TSZDcvlPtAnekTaItI3qO</EnqueuedURI><EnqueuedURIMetaData>&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;&lt;item id=&quot;0004206cspotify%3aalbum%3a1TSZDcvlPtAnekTaItI3qO&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;&lt;/dc:title&gt;&lt;upnp:class&gt;object.container.album.musicAlbum&lt;/upnp:class&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON3079_X_#Svc3079-0-Token&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;</EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>1</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>","simple":true,"resolveWithFullResponse":false,"transform2xxOnly":false},"response":{"statusCode":500,"body":"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>800</errorCode></UPnPError></detail></s:Fault></s:Body></s:Envelope>","headers":{"content-length":"347","content-type":"text/xml; charset=\"utf-8\"","ext":"","server":"Linux UPnP/1.0 Sonos/47.2-59120 (ZPS3)","connection":"close"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"192.168.1.16:1400","port":"1400","hostname":"192.168.1.16","hash":null,"search":null,"query":null,"pathname":"/MediaRenderer/AVTransport/Control","path":"/MediaRenderer/AVTransport/Control","href":"http://192.168.1.16:1400/MediaRenderer/AVTransport/Control"},"method":"POST","headers":{"SOAPAction":"\"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"","Content-type":"text/xml; charset=utf8","content-length":1152}}}}

Versions (and Environment)

Node version: 10.1.0 node-sonos version: 1.8.1 OS: MacOs

Villarrealized commented 5 years ago

From what I have seen, this error is raised when you do not have a Spotify Premium account connected to your Sonos system.

I was able to run your example and the song played fine for me.

I also tested another track and had success.

device.play("spotify:track:0aersVHlGGXcHeRvHtuaSt")
svrooij commented 5 years ago

@jeantristan could hou verify you have a premium spotify account. It might also be an issue with the Spotify region, you can set it to EU.

jeantristan commented 5 years ago

I managed to make it work! The problem was that the queue was not empty. Were you aware of that issue?

With this code it's working perfectly:

  const Sonos  = require('sonos')
  // Set up the master if a group, otherwise 500 error
  const device = new Sonos.Sonos('192.168.1.17')
  const spotify_id = req.params.spotify_id

  device.setSpotifyRegion(Sonos.SpotifyRegion.EU)

  const spotifyUri = 'spotify:album:' + spotify_id

  device.flush().then(result => {
        device.queue(spotifyUri).then(result => {
            device.selectQueue()
            device.play()
        });
    });
Villarrealized commented 5 years ago

Hmm, interesting. When I tested it, I had a non-empty queue and did not have any issues.

Coriou commented 5 years ago

Having the same problem, I can not use play(uri) on a non-empty queue (throws 500)