home-assistant-libs / pychromecast

Library for Python 3 to communicate with the Google Chromecast.
MIT License
2.53k stars 378 forks source link

Working with Alsa - analog input to cast speaker #319

Open mph070770 opened 4 years ago

mph070770 commented 4 years ago

Can anyone advise how to get analog audio from a Rpi (using a USB soundcard) output to a cast speaker? I've been trying to use mkchromecast (which uses pychromecast) to do this and I'm failing. My attempts are documented here:

https://github.com/muammar/mkchromecast/issues/279

If anyone could assist, it would be really appreciated. I'm not sure what I'm doing wrong.

edent commented 4 years ago

I've managed this! Sadly, it's a multistage process.

  1. Set up IceCast
  2. Set up ices2 to take ALSA input and sent it to IceCast
  3. Using PyChromeCast:
import time
import pychromecast
pychromecast.discovery.discover_chromecasts()

You'll get a list of ChromeCasts on your network. Mine showed:

[('192.168.0.789', 4321, UUID('5632e20d-1030-4885-ad3d-496504b7c820'), 'Pioneer VSX-933', 'Pioneer VSX-933 ABC1234')]

We want the ChromeCast's ID - which is the 4 digit int. Back to Python

pychromecast.get_chromecasts(4321)

You'll get a bit more debug data:

[Chromecast('192.168.0.789', port=4321, device=DeviceStatus(friendly_name='Pioneer VSX-933 ABC1234', model_name='Pioneer VSX-933', manufacturer='Onkyo And Pioneer', uuid=UUID('5632e20d-1030-4885-ad3d-496504b7c820'), cast_type='cast'))]

This supports cast - good!

Now we can send the audio to the ChromeCast

cast = pychromecast.get_chromecasts(4321)[0]
cast.wait()
mc = cast.media_controller
mc.play_media('http://192.168.0.123:8000/whatever.ogg', 'audio/ogg')