Playback
All playback methods are synchronous SOAP calls that return Result<(), SdkError> unless otherwise noted.
play(), pause(), and stop() also write the resulting PlaybackState into the cache on success; the
other transport calls leave the cache to the device’s own event.
Transport controls
Section titled “Transport controls”| Method | Description |
|---|---|
play() | Start or resume playback |
pause() | Pause playback |
stop() | Stop playback |
next() | Skip to next track |
previous() | Skip to previous track |
use sonos_sdk::prelude::*;
fn main() -> Result<(), SdkError> { let sonos = SonosSystem::new()?; let speaker = sonos.speaker("Living Room").unwrap();
speaker.play()?; speaker.pause()?; speaker.stop()?; speaker.next()?; speaker.previous()?;
Ok(())}Seek accepts a SeekTarget enum:
| Variant | Example | Description |
|---|---|---|
SeekTarget::Time(String) | "0:02:30" | Absolute position (H:MM:SS) |
SeekTarget::Track(u32) | 3 | Jump to track number (1-based) |
SeekTarget::Delta(String) | "+0:00:30" | Relative offset (prefix with +/-) |
// Seek to 2 minutes 30 secondsspeaker.seek(SeekTarget::Time("0:02:30".into()))?;
// Jump to track 3speaker.seek(SeekTarget::Track(3))?;
// Skip forward 30 secondsspeaker.seek(SeekTarget::Delta("+0:00:30".into()))?;
// Skip backward 10 secondsspeaker.seek(SeekTarget::Delta("-0:00:10".into()))?;Play mode
Section titled “Play mode”| Mode | Description |
|---|---|
Normal | Sequential playback |
RepeatAll | Repeat entire queue |
RepeatOne | Repeat current track |
Shuffle | Shuffle with repeat |
ShuffleNoRepeat | Shuffle without repeat |
ShuffleRepeatOne | Shuffle, repeat one |
speaker.set_play_mode(PlayMode::Shuffle)?;speaker.set_play_mode(PlayMode::RepeatAll)?;speaker.set_play_mode(PlayMode::Normal)?;Crossfade
Section titled “Crossfade”// Enable crossfade between tracksspeaker.set_crossfade_mode(true)?;
// Disable crossfadespeaker.set_crossfade_mode(false)?;
// Check current statelet crossfade = speaker.get_crossfade_mode()?;println!("Crossfade: {}", crossfade.crossfade_mode);Set transport URI
Section titled “Set transport URI”Controls what the speaker plays. Used behind the scenes for queues, radio, line-in, and grouping.
// Play a radio streamspeaker.set_av_transport_uri( "x-rincon-mp3radio://streams.example.com/jazz", "", // metadata (DIDL-Lite XML, optional))?;speaker.play()?;
// Play from the speaker's queuespeaker.set_av_transport_uri("x-rincon-queue:RINCON_XXX#0", "")?;speaker.play()?;Gapless playback
Section titled “Gapless playback”Pre-load the next source so the speaker transitions seamlessly:
speaker.set_next_av_transport_uri( "x-rincon-mp3radio://streams.example.com/next", "",)?;Query available actions
Section titled “Query available actions”Check what actions are valid in the current state (depends on source type):
let actions = speaker.get_current_transport_actions()?;println!("Available: {}", actions.actions);// e.g., "Play,Pause,Stop,Next,Previous,Seek"Reactive playback state
Section titled “Reactive playback state”The playback_state property is watchable. Acquire the handle before the loop —
sonos.iter() only emits events for properties already under watch:
let state = speaker.playback_state.watch()?;
for _event in sonos.iter() { println!("State: {:?}", state.value());}Current track info
Section titled “Current track info”CurrentTrack fields are all Option<String>, because a stream may supply any
subset of them. display() formats whatever is present as "artist - title",
falling back to whichever half exists.
let track = speaker.current_track.fetch()?;println!("{}", track.display());println!("album: {:?}, art: {:?}", track.album, track.album_art_uri);album_art_uri is whatever the speaker reported, which is often a path on the
speaker (/getaa?s=1&u=...) rather than a full URL. album_art_url(ip) returns
an absolute URL either way, resolving a path against the speaker that reported
the track:
if let Some(url) = track.album_art_url(speaker.ip) { println!("art: {url}");}track.source() classifies the track URI into a PlaybackSource. A speaker
grouped under a coordinator reports PlaybackSource::GroupFollower and carries
no track of its own, so read the coordinator instead:
use sonos_sdk::PlaybackSource;
if track.source() == Some(PlaybackSource::GroupFollower) { // ask speaker.group() for its coordinator's current_track}Watch for track changes:
let track = speaker.current_track.watch()?;
for _event in sonos.iter() { if let Some(t) = track.value() { println!("Now playing: {}", t.display()); }}Position
Section titled “Position”Position reports milliseconds, and progress() turns it into a 0.0–1.0
fraction for a progress bar:
let pos = speaker.position.fetch()?;println!("{}ms of {}ms ({:.0}%)", pos.position_ms, pos.duration_ms, pos.progress() * 100.0);Speakers don’t include the position in their events, so a watched Position
doesn’t tick on its own. The SDK fetches it when the watch starts and again
whenever the track or play state changes. In between, the value is a snapshot:
to show a moving progress bar, add the time elapsed since the last update
while the speaker is playing.
let position = speaker.position.watch()?;// position.value() updates on track changes, play, pause and stop.