Table of Contents |
---|
Install Sonos Node Library
...
Code Block |
---|
mkdir sonosPlayersonos cd sonosPlayersonos npm init npm install sonos npm install git://github.com/bencevans/node-sonos.git |
Scripts
Discover.js
> vi discover.js
Code Block |
---|
const {Sonos DeviceDiscovery } = require('sonos') // event on all found... DeviceDiscovery((deviceconsole.log('Searching for Sonos devices for 5 seconds...') const discovery = new Sonos.AsyncDeviceDiscovery() discovery.discover().then((device, model) => { console.log('foundFound one sonos device %s getting atall groups', + device.host) return // mute every device... device.setMuted(true)getAllGroups().then((groups) => { console.thenlog(d => console.log(`${d.host} now muted`)) }) // find one device DeviceDiscovery().once('DeviceAvailable', (device) => { console.log('found device at ' + device.host) // get all groups sonos = new Sonos(device.host) sonos.getAllGroups().then(groups'Groups %s', JSON.stringify(groups, null, 2)) }) }).catch(e => { console.warn(' Error in discovery %j', e) }) |
Run it
> node discover.js
PlayRadioParadise.js
vi playRadioParadise.js
Code Block |
---|
const Sonos = require('sonos').Sonos const sonos = new Sonos(process.env.SONOS_HOST || '192.168.1.26') // This example demonstrates playing radio staions // the Sonos built-in support for tunein radio. // CAUTION: ID is with leading s since version 1.12.1 const stationId = 's13606' const stationTitle = 'Radio Paradise' sonos.playTuneinRadio(stationId, stationTitle).then(success => { console.log('Yeay') }).catch(err => groups{ console.forEach(group =>log('Error occurred %j', err) }) |
Run it
> node playRadioParadise.sh
Run It using environment variables
> SONOS_HOST=192.168.1.21 node playRadioParadise.sh
Add Start Script
If you want to add your program to npm, edit the package.json file
Code Block |
---|
{ "name": "sonosplayer", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node console.log(group.Name); }) }) }) |
Run it
discover.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"sonos": "git://github.com/bencevans/node-sonos.git"
}
} |
Run it
> npm start> node discover.js
References
Reference | URL |
---|---|
node-sonos | https://www.npmjs.com/package/sonos |