#sonos #speaker #music #action

rusty-sonos

A library for communicating with and discovering Sonos speakers

8 releases

0.2.5 Mar 30, 2024
0.2.4 Dec 31, 2023
0.1.2 Dec 20, 2023

#7 in #speaker

Download history 3/week @ 2024-07-24 29/week @ 2024-07-31 2/week @ 2024-09-18 3/week @ 2024-09-25 2/week @ 2024-10-02

412 downloads per month

MIT license

35KB
692 lines

Rusty-Sonos

crates.io Documentation MIT licensed

A library for discovering and interacting with Sonos speakers, written in Rust.

Features

The primary functionality of this library is to provide a wrapper for speaker discovery and speaker actions.

Discovery

To discover all speakers on the current network, use discover_devices(). This will return basic information about speakers (including IP addresses) about any speakers that were found. Internally, this uses the SSDP protocol.

To get information about a specific speaker, given its IP, use get_speaker_info().

Speaker Interaction

Interaction with speakers is done through a single struct, Speaker, which has methods for all the features that are currently implemented. To use Speaker, you must know the speaker's IP address (refer to the discovery section for how to find this):

use rusty_sonos::speaker::Speaker;
use std::net::Ipv4Addr;

let ip_addr = Ipv4Addr::from_str("192.168.1.0").unwrap();

let speaker = Speaker::new(ip_addr).await.unwrap();

speaker.play().await.unwrap(); // plays the current track

A non-exhaustive list and description of speaker methods is provided below:

  • play: starts or resumes playback of the current track
  • pause: pauses playback of the current track
  • get_current_track: returns information about the current track
  • set_current_uri: sets the current track from a URI
  • get_volume: returns the current volume
  • set_volume: sets the volume to the given value
  • get_playback_status: gets the playback status (playing, paused, etc.)
  • seek: starts playback from the provided time in the track (hh:mm:ss)
  • get_queue: returns the tracks currently in the queue
  • enter_queue: enters the queue
  • add_track_to_queue: adds a track to the queue
  • move_to_next_track: skips to the next track
  • move_to_previous_track: moves to the previous track
  • clear_queue: removes all tracks from the queue
  • end_external_control: ends control of the speaker by external services, such as Spotify

Notes

Generally, the speakers' API is UPnP-based

These unofficial docs were used to build this library. They contain information about various services, endpoints, and responses.

Dependencies

~4–16MB
~209K SLoC