Giter Site home page Giter Site logo

Comments (3)

postlund avatar postlund commented on August 28, 2024

I don't think you can run the asyncio event loop from another thread (at least not yet). So I would say that is incorrect usage of asyncio. What are you trying to accomplish?

from pyatv.

xXxNIKIxXx avatar xXxNIKIxXx commented on August 28, 2024

I want to have an list of all HomePods and the can select one or multiple HomePods, were an live audio stream of my system Audio is played. I have found an workaround, wich is not that nice, but it works. I create like an thread with another process in it. And then it works.

from pyatv.

postlund avatar postlund commented on August 28, 2024

You can do that in asyncio, but you don't use threads. Instead use tasks. Something like this will stream to all HomePods and wait for it to finish:

from pyatv import scan, connect, const
import asyncio
import asyncio.subprocess as asp

TARGET_MODELS = [const.DeviceModel.HomePod, const.DeviceModel.HomePodGen2, const.DeviceModel.HomePodMini]

async def stream_to_device(device):
    loop = asyncio.get_event_loop()

    atv = await connect(device, loop)
    
    process = await asp.create_subprocess_exec("C:\\Program Files\\ffmpeg\\ffmpeg.exe", "-f", "dshow", "-i", "audio=Home Pod (VB-Audio Virtual Cable)", "-acodec", "libmp3lame", "-f", "mp3", "-", stdout=asp.PIPE, stderr=None)
    await atv.stream.stream_file(process.stdout)


async def main():
    devices = await scan(asyncio.get_event_loop())
    streams = []
    for device in devices:
        if device.device_info.model in TARGET_MODELS:
            streams.append(asyncio.create_task(stream_to_device(device)))

    await asyncio.gather(*streams)

if __name__ == "__main__":
    asyncio.run(main())

I have not tested this, but should be mostly correct.

from pyatv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.