nativescript-sonos
by Todd Anglin | v0.5.1
Control Sonos systems from NativeScript apps
npm i --save nativescript-sonos

NativeScript Sonos Plugin (WIP)

This plugin provides an interface for controlling a Sonos system on a local network from a NativeScript app. It is influenced heavily by the NodeJS Sonos library created by Ben Evans.

This is an incomplete, work-in-process plugin. Use accordingly.

Run the demo

After cloning this repo, navigate to the repo folder in your terminal and run this command:

$ npm run demo.ios

This will build the lastest version of the plugin, add it to the demo project and launch the demo in the emulator.

NOTE: The demo has only been tested for iOS, but the plugin should work on both iOS and Android.

How to use this plugin?

Add the plugin to a NativeScript application from the terminal:

$ tns plugin add nativescript-sonos

In your app, initialize the plugin with a valid Sonos IP address:

import { Sonos } from "nativescript-sonos"

let sonos = new Sonos("10.0.1.188"); //Replace with your Sonos IP address

You can now call methods to control a Sonos player. Unlike the original NodeJS library, this plugin uses Promises for all async methods. For example, to Play:

this.sonos.play()
.then(() => {
// Command to play completed successfully
})
.catch((err) => {
// Something went wrong; handle the error state
});

Supported operations

This plugin does not yet support all of the methods from the original NodeJS library. Currently implemented methods are:

  • currentTrack: () => Promise;
  • selectTrack: (trackNr?: number) => Promise;
  • getVolume: () => Promise;
  • setVolume: (volume: number) => Promise;
  • getMuted: () => Promise;
  • setMuted: (muted: boolean) => Promise;
  • play: (uri?: string | UriObject) => Promise;
  • stop: () => Promise;
  • pause: () => Promise;
  • seek: (seconds: number) => Promise;
  • next: () => Promise;
  • previous: () => Promise;
  • flush: () => Promise;
  • becomeCoordinatorOfStandaloneGroup: () => Promise;
  • setName: (name: any) => Promise;
  • setPlayMode: (playmode: PlayModeEnum) => Promise;
  • selectQueue: () => Promise;
  • queue: (uri: string | UriObject, positionInQueue?: number) => Promise;
  • getCurrentState: () => Promise;
  • getZoneInfo: () => Promise;
  • getZoneAttrs: () => Promise;
  • getZoneGroupState: () => Promise<SonosZoneGroup[]>;
  • getLEDState: () => Promise;
  • setLEDState: (setStateOn: boolean) => Promise;
  • getTopology: () => Promise;
  • getZonesWithDescriptions: () => Promise<SonosZone[]>;
  • deviceDescription: (host?: string, port?: number) => Promise;
  • getQueue: () => Promise;
  • getMusicLibrary: (searchType: SonosSearchType, startIndex?: number, pageSize?: number) => Promise;
  • searchMusicLibrary: (searchType: SonosSearchType, searchTerm: string, startIndex?: number, pageSize?: number) => Promise;
  • getFavoritesRadioStations: (startIndex?: number, pageSize?: number) => Promise;
  • getFavoritesRadioShows: (startIndex?: number, pageSize?: number) => Promise;
  • getFavoritesRadio: (favoriteRadioType: any, startIndex?: number, pageSize?: number) => Promise;
  • playTuneinRadio: (stationId: any, stationTitle: any) => Promise;
  • addSpotify: (trackId: any) => Promise;
  • playSpotifyRadio: (artistId: any, artistName: any) => Promise;
  • addSpotifyQueue: (trackId: any) => Promise;

Sonos device discovery APIs

This plugin does not yet support the Discovery APIs (allowing you to automatially locate Sonos devices on the network) due to the lack of support in NativeScript for UDP socket connections.

When UDP socket connections become possible, Discovery APIs should be possible in NativeScript.

Contributions welcome. 😀

What's next?

Additional Sonos APIs from the NodeJS library will be exposed in this plugin. If there is an API you need that is not yet exposed, open an Issue and we'll try to prioritize it.

Contributions and constructive feedback welcome.

License

MIT. Use it, transform it, whatever.