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.
- Host: GitHub
- URL: https://github.com/kilianb/java-sonos-controller
- Owner: KilianB
- License: other
- Created: 2018-04-28T16:04:59.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-08-30T12:25:43.000Z (almost 4 years ago)
- Last Synced: 2025-04-28T17:46:35.501Z (about 1 year ago)
- Topics: controller, java, sonos, upnp
- Language: Java
- Homepage:
- Size: 390 KB
- Stars: 12
- Watchers: 2
- Forks: 7
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
Awesome Lists containing this project
README
[  ](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.
### 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.

## Original contribution
A fork of the tremendous sonos controller library originally created by Valentin Michalak.
Based upon this changes include:
- Implementing UPnP event subscriptions
- Swapping out gradle
- 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 ....