How can I subscribe `audio` programmaticaly/manual...
# ask-ai
s
How can I subscribe
audio
programmaticaly/manually? Instead of doing this?
Copy code
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
t
Thanks for reaching out, Goktug. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you’re asking how to subscribe to audio tracks manually (instead of using
ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
). You can do that by disabling auto-subscribe at connect time and then subscribing to audio publications yourself when they appear. Below is a concise pattern that gives you full control. 1. Disable auto-subscribe when connecting
Copy code
await ctx.connect(auto_subscribe=False)
1. Subscribe when a track is published
Copy code
@ctx.room.on("track_published")
def on_track_published(publication: RemoteTrackPublication, participant: RemoteParticipant):
    if publication.kind == TrackKind.AUDIO:
        # subscribe asynchronously so you don't block the event handler
        asyncio.create_task(publication.set_subscribed(True))
1. Subscribe to already-published tracks when a participant joins
Copy code
@ctx.room.on("participant_connected")
def on_participant_connected(participant: RemoteParticipant):
    for publication in participant.track_publications.values():
        if publication.kind == TrackKind.AUDIO:
            asyncio.create_task(publication.set_subscribed(True))
Notes and tips • Use
publication.set_subscribed(True)
to toggle subscription on a given
RemoteTrackPublication
. • Run the subscription call in a background task (e.g.,
asyncio.create_task
) inside event handlers to avoid blocking the event loop. • You can add custom logic (participant identity, roles, or other conditions) before calling
set_subscribed(True)
to control who you subscribe to. If you’d like, tell me: • which SDK/language you’re using if it’s not Python, and • any selection rules (e.g., subscribe only to hosts or specific participant IDs), and I’ll give a tailored example. Sources: Live Conversations with AI using ChatGPT and WebRTC