Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gwpl/4efd5933fbf9015ee36cd0e443bfea39 to your computer and use it in GitHub Desktop.
Save gwpl/4efd5933fbf9015ee36cd0e443bfea39 to your computer and use it in GitHub Desktop.
How to play linux default audio output on Sonos speakers (with delay using http mp3 or flac streaming)

how tu setup pulseaudio->streaming via cvlc (command line vlc -> so should simmilarily work with gui version (in my case don't)): https://superuser.com/a/1021823/81861

get pulseaudio source from list:

pactl list | grep "Monitor Source"

and assign to P , e.g. in my case it's usually:

P=pulse://combined.monitor

Then run one of following cvlc:

flac:

cvlc -vvv "$P" --sout '#transcode{vcodec=none,acodec=flac,ab=1441,channels=2,samplerate=44100,scodec=none}:standard{access=http,dst=0.0.0.0:8888/pc.flac}'

or mp3:

cvlc -vvv "$P" --sout '#transcode{acodec=mp3,ab=128,channels=2}:standard{access=http,dst=0.0.0.0:8888/pc.mp3}'

Now let's use http://python-soco.com/ to insert our new http://192.168.0.111:8888/pc.flac (or http://192.168.0.111:8888/pc.mp3) on playlist! (you may use other tool for that, maybe some other sonos controller will allow you to provide url)

Now , modifying my script to play flac files from flocal serwer ( https://gist.github.com/gwpl/92ab540016bf43359654d5b0f013b1ec )

argv[1] = speaker ip (you can get with socos list) assuming you used flac cvlc command, otherwise change line to s.add_uri_to_queue('http://192.168.0.111:8888/pc.mp3'):

#!/usr/bin/env python3
import sys
import soco

ip=sys.argv[1]
print('Playing on Speaker with ip='+ip)
s=soco.SoCo(ip)
print(' get_speaker_info()='+str(s.get_speaker_info()))

s.clear_queue()
s.add_uri_to_queue('http://192.168.0.111:8888/pc.flac')
s.play_from_queue(0)

When nothing feeds input, then stream gets stuck and sonos speakers give up -> very annying when I try to move video to particular point as it creates short breaks in audio stream then whole setup does not hangle smoothly. Forutnatelly there is solution! Play in background silence in a loop (command to play in a loop from command line taken from https://askubuntu.com/a/316048/31300 ):

ffplay -nodisp -autoexit -loop 9999 silence_10s.wav
@antonylesuisse
Copy link

I did a more generic/polished version of that solution.

https://github.com/antonylesuisse/sonos

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