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') const { Sonos } = require('sonos') // event on all found... DeviceDiscovery((device console.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 atgetting all groups' +, device.host) // mute every return 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 => { groups.forEach(groupconsole.log('Yeay') }).catch(err => { console.log(group.Name); }) 'Error occurred %j', err) }) }) |
Run it
> node playRadioParadise.sh
Run It using environment variables
> node discover.jsSONOS_HOST=192.168.1.21 node playRadioParadise.sh
Add Start Script
If you want to add it your program to npm, edit the package.json file
Code Block |
---|
{ "name": "sonosplayer", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node 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
References
Reference | URL |
---|---|
node-sonos | https://www.npmjs.com/package/sonos |