An open API service indexing awesome lists of open source software.

https://github.com/kilianb/java-sonos-controller

Sonos controller api with UPnP event support. Examples show text to speech on speakers and local music file playback.
https://github.com/kilianb/java-sonos-controller

controller java sonos upnp

Last synced: 11 months ago
JSON representation

Sonos controller api with UPnP event support. Examples show text to speech on speakers and local music file playback.

Awesome Lists containing this project

README

          

[ ![Download](https://api.bintray.com/packages/kilianb/maven/Java-Sonos-Controller/images/download.svg) ](https://bintray.com/kilianb/maven/Java-Sonos-Controller/_latestVersion)

# Sonos-controller
Java API for controlling [SONOS](http://www.sonos.com/) players.



## Available via Bintray and JCenter

Starting with version 2.0.0 at least Java 10 is required.

```


jcenter
https://jcenter.bintray.com/

github.com.kilianB
sonos-controller
2.0.0

```

## Basic Usage

Discovery all Sonos Devices on your network.

```java
List devices = SonosDiscovery.discover();

//Asynchronous
SonosDiscovery.discoverAsynch(3, device ->{
System.out.println("Device: " + device + " found");
});

```

Connect to a known Sonos and pause currently playing music.

```java
SonosDevice sonos = new SonosDevice("10.0.0.102");
sonos.pause();
```

## UPnP Event Handling

Register event handlers to gain immediate access to update events

```java
sonos.registerSonosEventListener(new SonosEventAdapter() {

@Override
public void volumeChanged(int newVolume) {
System.out.println("Volume changed: " + newVolume);
}

@Override
public void playStateChanged(PlayState newPlayState) {
System.out.println("Playstate changed: " + newPlayState);
}

@Override
public void trackChanged(TrackInfo currentTrack) {
System.out.println("Track changed: " + currentTrack);
}
}
```

Gain full access by utilizing the entire range of callback methods found in the [SonosEventListener.java](https://github.com/KilianB/Java-Sonos-Controller/blob/master/src/main/java/com/github/kilianB/sonos/listener/SonosEventListener.java).

## More examples

### 1. Text to speech playback on any sonos speakers

A small example utilizing my prototyping text to speech library.
The generated mp3 file is hosted on the current machine to make it accessible to the sonos speakers on the network.

Source



### 2. Simple Sonos Desktop Player With Local File Playback

A basic player allowing to playback local music files. Index any folder on your computer, create a track index in a SQL
table and make the folders accessible to the network. While you are able to start stop, playback change volume etc,
this is just intended to lay out the steps needed to implement local music file playback and not function as a standalone application.


  1. Directory Crawler & File Indexer

  2. SQL Database

  3. File Hosting

![sonosspeaker](https://user-images.githubusercontent.com/9025925/46569592-b8871b80-c957-11e8-9095-d4310b4c977b.jpg)

## Original contribution

A fork of the tremendous sonos controller library originally created by Valentin Michalak.

Based upon this changes include:


  1. Implementing UPnP event subscriptions

  2. Swapping out gradle

  3. License change to GPLv3

This repository allows to subscribe to the UPnP 1.1 Event endpoints enabling to receive continious updates of the devices state. I decided to fork the project instead of issuing a pull request due to the need of it being hosted via maven central within a (short) period of time. A huge portion of the code was being rewritten resulting in breaking changes and no backward compatibility If you look for a MIT version or high test coverage either contact me or take a look at the original repository.

## Up next

Investigate the UPnP event endpoints.


  • /MediaServer/ConnectionManager/Event

  • /MediaRenderer/ConnectionManager/Event

  • /MediaServer/ContentDirectory/Event

  • /AlarmClock/Event

  • /MusicServices/Event

  • /SystemProperties/Event

How are topology changes best are tracked? (New device found / device disconnected)
Currently the library utilizes the `/ZoneGroupTopology/Event` as does the official client.
But updates are delayed as much as two minutes.
A different approach would be to either:


  • Timeout if no upnp advertisement is send within a certain timeframe?

  • Parse topology page of a sonos controler e.g. http://192.168.178.26:1400/status/topology - polling ....