Skip to content

Instantly share code, notes, and snippets.

@denysvitali
Last active November 12, 2024 01:54
Show Gist options
  • Save denysvitali/6d15e0cab8c4851d76ca19356d30be05 to your computer and use it in GitHub Desktop.
Save denysvitali/6d15e0cab8c4851d76ca19356d30be05 to your computer and use it in GitHub Desktop.
Quick tutorial on how to play sound from your computer to a Sonos device

Quick tutorial on how to play sound from your computer to a Sonos device

Introduction

Assumptions

192.168.1.111 IP of your Sonos Device
192.168.1.128 IP of your computer

Configuration

PulseAudio

Append to /etc/pulse/default.pa:

load-module module-null-sink sink_name=rtp channels=2 rate=44100 
load-module module-rtp-send source=rtp.monitor destination=127.0.0.1 port=46998 loop=1
set-default-sink rtp

Restart PulseAudio with pulseaudio -k (if pulseaudio doesn't turn itself on start it with pulseaudio -D)

Streaming

Select one of these two methods. Try them and see which one performs better

Method 1

VLC (cvlc)

cvlc --live-caching 800 "rtp://@127.0.0.1:46998" --sout "#transcode{vcodec=none,acodec=mp3,ab=256,channels=2,samplerate=44100}:http{dst=:8554/audio.mp3}"

Then play with URI http://192.168.1.122:8554/audio.mp3.

Method 2

ffmpeg + cvlc (New method, works better in my case)

ffmpeg -i 'rtp://127.0.0.1:46998' -q:a 0 -map a -f mp3 pipe:1 | cvlc -vvv - --live-caching 500 --sout '#standard{access=http,mux=mp3,dst=0.0.0.0:8554}'

Then play with URI x-rincon-mp3radio://192.168.1.128:8554/.

Firewall Rules

iptables

sudo iptables -I INPUT -p tcp --dport 8554 -j ACCEPT

Media playing

SoCo

.play_uri(URI) argument must be either http://192.168.1.122:8554/audio.mp3 or x-rincon-mp3radio://192.168.1.128:8554/ - depending on which method you chose above.

from soco import SoCo
my_zone = SoCo('192.168.1.111')
my_zone.play_uri('http://192.168.1.128:8554/audio.mp3')

Enjoy 🎵

@x0rrb
Copy link

x0rrb commented Jul 14, 2024

I found a shorted path. Tested on Debian 6.1.76-1.

Identify the audio interface to stream:

pactl list sources short
2224	alsa_output.pci-0000_05_00.6.analog-stereo.2.monitor	PipeWire	s32le 2ch 48000Hz	RUNNING
2225	alsa_input.pci-0000_05_00.6.analog-stereo.2	PipeWire	s32le 2ch 48000Hz	SUSPENDED
2834	alsa_output.pci-0000_05_00.1.hdmi-stereo.2.monitor	PipeWire	s32le 2ch 48000Hz	SUSPENDED

With cvlc define the input interface on pulse://interface_name and define the stdout for mp3 and http.

cvlc pulse://alsa_output.pci-0000_05_00.6.analog-stereo.2.monitor \
--sout "#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:http{mux=raw,dst=:8080/}" --no-sout-video

With sonos-cli:

sonos <SONOS-DEVICE> play_uri "http://<DESKTOP-IP>:8080/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment