Java API for controlling SONOS players.
Starting with version 2.0.0 at least Java 10 is required.
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependency>
<groupId>github.com.kilianB</groupId>
<artifactId>sonos-controller</artifactId>
<version>2.0.0</version>
</dependency>
Discovery all Sonos Devices on your network.
List<SonosDevice> devices = SonosDiscovery.discover();
//Asynchronous
SonosDiscovery.discoverAsynch(3, device ->{
System.out.println("Device: " + device + " found");
});
Connect to a known Sonos and pause currently playing music.
SonosDevice sonos = new SonosDevice("10.0.0.102");
sonos.pause();
Register event handlers to gain immediate access to update events
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.
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.
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.
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.
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 ....