Skip to content

Can you stream to multiple AirPlay devices? #2832

@justjam2013

Description

@justjam2013

What do you need help with?

I would like to stream to multiple AirPlay devices. For multiple songs streamed to a single device, I do this:

            for song_path in song_files:
                self.logger.info(f"Playing {song_path}")
                await self.atv.stream_file(song_path, metadata)
                await asyncio.sleep(1)

Which is fine becasue the songs are sequential. So I thought about something similar to this:

atvs[] = await pyatv.scan(self.loop, identifier={"id1", "id2"}, timeout=5)
for atv in atvs:
    await pyatv.connect(atv, loop)
...
for atv in atvs:
    await atv.stream.stream_file("/path/file.mp3")
...
for atv in atvs:
    atv.close()

Except the await is blocking until the file is fully streamed, so each AirPlay device would play in sequence.

The multithreaded way of doing it would be like this:

import threading
...
def worker(atv):
    atv.stream.stream_file("/path/file.mp3")
...
threads = []

# Create and start threads
for atv in atvs:
    t = threading.Thread(target=worker, args=(atv,))
    t.start()
    threads.append(t)

# Wait for all threads to finish
for t in threads:
    t.join()

This takes care of streaming to to multiple devices, but there is no guarantee that the sound would be synchronized across all devices, how AirPlay does it. So how would I do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions