bencevans / node-sonos

🔈 Sonos Media Player Interface/Client

Home Page:https://www.npmjs.com/package/sonos

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to play local file, but online file works.

Stanned opened this issue · comments

Expected Behavior

Play local file on speaker

Current Behavior

Gives error when giving local file url:
Error: upnp: statusCode 500 & upnpErrorCode : <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>804</errorCode></UPnPError></detail></s:Fault></s:Body></s:Envelope> at request.then.then.then.catch (C:\Users\stank\Documents\P2000 Alert\node_modules\sonos\lib\services\Service.js:87:25) at process._tickCallback (internal/process/next_tick.js:68:7)

Sample code or executed example

Working:
sonos.play("https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3").then((result) => { console.log(result); }).catch((err) => { console.log(err); })

Not working and producing error:
sonos.play(__dirname + "\\voice.mp3").then((result) => { console.log(result); }).catch((err) => { console.log(err); })

Versions (and Environment)

Node version: v10.16.3
node-sonos version: 1.14.0
OS: Windows 10

I tried almost everything, but with no succes. There's probably a really simple solution but I just can't seem to get it to work.

This library controls the sonos speaker over the network. It can only play files it knows. While it is possible to connect your sonos speakers to a samba file share that is pretty difficult to do.

You need to start a web server on your local machine and then host the mp3 there.

If your use case is using some text-to-speech to mp3 and then playing on the sonos device, you might be better of with https://github.com/svrooij/node-sonos-tts-polly

After some attempts I made a webserver to generate the TTS files and then serve them. It works perfectly, but not with the Sonos speaker.
The url I am now using is

http://play.stanexe.com:3896/tts?text=Test&key=MYS5Qe68XnqtUjJFSSptTreWuXzKEZGM

Now this still refuses to play the sound, even though the url from archive.org still works.
I am still using the examples/playthis.js file, but no result. The error I get is:
Error: upnp: statusCode 500 & upnpErrorCode <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>804</errorCode></UPnPError></detail></s:Fault></s:Body></s:Envelope>

Any idea why it is still not working? The url is fully accessible from all my other devices and is hosted on an external server, like the archive.org url.

New code:

const Sonos = require('sonos').Sonos
const sonos = new Sonos(process.env.SONOS_HOST || '192.168.2.12')

sonos.play('http://play.stanexe.com:3896/tts?text=Test&key=MYS5Qe68XnqtUjJFSSptTreWuXzKEZGM').then(success => {
  console.log('Yeay')
}).catch(err => { console.log(err) })

Sonos needs an url to a file it recognizes as a song. To my knowledge it needs to end with .mp3. So if you can change the url to be like http://play.standee.com:3896/tts/{text+here}/tts.mp3 with url rewriting or just changing the endpoint, it should work.

also it has to respond quick enough for Sonos not to skip the song.

you can also check-out this package it allows you to just call an endpoint that will return the url of the generated mp3. Which can be played for sure on your Sonos speakers.

I got it working using the method you said. Thank you!