Skip to content

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

License

Notifications You must be signed in to change notification settings

deamn/Java-Sonos-Controller

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Download

Sonos-controller

Java API for controlling SONOS players.

Available via Bintray and JCenter

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>

Basic Usage

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();

UPnP Event Handling

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.

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

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:

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 99.8%
  • CSS 0.2%