Skip to content

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.

MethodDescription
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:

VariantExampleDescription
SeekTarget::Time(String)"0:02:30"Absolute position (H:MM:SS)
SeekTarget::Track(u32)3Jump to track number (1-based)
SeekTarget::Delta(String)"+0:00:30"Relative offset (prefix with +/-)
// Seek to 2 minutes 30 seconds
speaker.seek(SeekTarget::Time("0:02:30".into()))?;
// Jump to track 3
speaker.seek(SeekTarget::Track(3))?;
// Skip forward 30 seconds
speaker.seek(SeekTarget::Delta("+0:00:30".into()))?;
// Skip backward 10 seconds
speaker.seek(SeekTarget::Delta("-0:00:10".into()))?;
ModeDescription
NormalSequential playback
RepeatAllRepeat entire queue
RepeatOneRepeat current track
ShuffleShuffle with repeat
ShuffleNoRepeatShuffle without repeat
ShuffleRepeatOneShuffle, repeat one
speaker.set_play_mode(PlayMode::Shuffle)?;
speaker.set_play_mode(PlayMode::RepeatAll)?;
speaker.set_play_mode(PlayMode::Normal)?;
// Enable crossfade between tracks
speaker.set_crossfade_mode(true)?;
// Disable crossfade
speaker.set_crossfade_mode(false)?;
// Check current state
let crossfade = speaker.get_crossfade_mode()?;
println!("Crossfade: {}", crossfade.crossfade_mode);

Controls what the speaker plays. Used behind the scenes for queues, radio, line-in, and grouping.

// Play a radio stream
speaker.set_av_transport_uri(
"x-rincon-mp3radio://streams.example.com/jazz",
"", // metadata (DIDL-Lite XML, optional)
)?;
speaker.play()?;
// Play from the speaker's queue
speaker.set_av_transport_uri("x-rincon-queue:RINCON_XXX#0", "")?;
speaker.play()?;

Pre-load the next source so the speaker transitions seamlessly:

speaker.set_next_av_transport_uri(
"x-rincon-mp3radio://streams.example.com/next",
"",
)?;

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"

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

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 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.