Quick Start
Your first program
Section titled “Your first program”This example discovers your Sonos network, plays music on a speaker, and reads its volume:
use sonos_sdk::prelude::*;
fn main() -> Result<(), SdkError> { // Discovers all speakers on your network let sonos = SonosSystem::new()?;
// Get a speaker by name let speaker = sonos.speaker("Kitchen").unwrap();
// Control playback speaker.play()?;
// Read properties directly from the device let volume = speaker.volume.fetch()?; println!("Kitchen is playing at {}%", volume.value());
Ok(())}Every property is a typed newtype rather than a bare integer, so unwrap it with
the accessor — Volume::value(), Mute::is_muted(), Bass::value() — or match
on it. The types carry Debug but not Display, so {:?} works and {} does
not.
Three ways to read properties
Section titled “Three ways to read properties”Every property on a speaker supports three access patterns:
// 1. get() — instant, returns the cached value (None if never observed)let cached: Option<Volume> = speaker.volume.get();
// 2. fetch() — makes a live SOAP call to the devicelet live: Volume = speaker.volume.fetch()?;
// 3. watch() — subscribes to real-time events; the handle reads livelet volume = speaker.volume.watch()?;println!("Volume right now: {:?}", volume.value());
// Hold that one handle: value() re-reads on every call, so there is no// need to watch again to refresh it.for _event in sonos.iter() { println!("Volume: {:?}", volume.value());}| Method | Speed | Freshness | Use when |
|---|---|---|---|
get() | Instant | May be stale | Displaying cached state in a UI |
fetch() | ~10ms | Always fresh | Need the current value right now |
watch() | Real-time | Live stream | Reacting to changes as they happen |
Navigate between speakers and groups
Section titled “Navigate between speakers and groups”let sonos = SonosSystem::new()?;
// Speakers → Groupslet kitchen = sonos.speaker("Kitchen").unwrap();let group = kitchen.group().unwrap();println!("Kitchen is in: {}", group.id);
// Groups → Speakersfor speaker in group.members() { println!(" - {}", speaker.name);}Speaker::name, Speaker::id, Speaker::ip, Speaker::model_name,
Group::id, Group::coordinator_id, and Group::member_ids are plain public
fields, not methods.
Next steps
Section titled “Next steps”- Architecture — understand the layered design
- Properties — deep dive into get/fetch/watch
- Playback — transport controls, seeking, play modes
- Cookbook — common recipes