bulky-wall-61623
09/08/2025, 1:20 PMtall-airline-16756
09/08/2025, 1:20 PMtall-airline-16756
09/08/2025, 1:20 PMbulky-wall-61623
09/08/2025, 1:22 PMtall-airline-16756
09/08/2025, 1:22 PMbulky-wall-61623
09/08/2025, 1:24 PMtall-airline-16756
09/08/2025, 1:24 PMsession.output.set_audio_enabled() method on your server-side agent. Let me walk you through how to set this up:
1. First, set up an RPC method in your agent: This method will listen for toggle commands coming from the frontend. Here's an example:
@room.local_participant.register_rpc_method("toggle_output")
async def on_toggle_output(data: rtc.RpcInvocationData) -> None:
if data.payload == "audio_on":
session.output.set_audio_enabled(True)
elif data.payload == "audio_off":
session.output.set_audio_enabled(False)
1. Next, send an RPC request from the frontend: Use the participant's RPC functionality to send a command to the agent like this:
await localParticipant.rpc("toggle_output", "audio_off"); // or "audio_on"
And that's it! This way, you can toggle the audio output on or off without needing to restart your session. When you flip _audio_enabled_, the agent smoothly handles the audio sink behind the scenes, making everything feel seamless.
Sources: Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | examples/avatar_agents/audio_wave/agent_worker.py | agents/src/voice/agent_session.tsbulky-wall-61623
09/08/2025, 1:25 PMtall-airline-16756
09/08/2025, 1:26 PMimport logging
from livekit import rtc
from livekit.agents import Agent, AgentSession, JobContext, WorkerOptions, cli
from livekit.plugins import openai
logger = logging.getLogger("dynamic-audio-agent")
logger.setLevel(logging.INFO)
async def entrypoint(ctx: JobContext):
# Let’s initialize the agent session
session = AgentSession(
llm=openai.realtime.RealtimeModel(),
)
# Start the session with some initial RoomOutputOptions
await session.start(
agent=Agent(instructions="You are a helpful assistant."),
room=ctx.room,
room_output_options=RoomOutputOptions(
audio_enabled=True, # Starting with audio on
transcription_enabled=True,
),
)
# Here’s an RPC method to toggle the audio output on and off
@ctx.room.local_participant.register_rpc_method("toggle_audio_output")
async def on_toggle_audio_output(data: rtc.RpcInvocationData) -> None:
current_state = session.output.audio_enabled
new_state = not current_state
logger.info(
"Toggling audio output",
extra={"caller_identity": data.caller_identity, "payload": data.payload, "from": current_state, "to": new_state},
)
# This is where we actually switch the audio state
session.output.set_audio_enabled(new_state)
# And here’s an optional RPC method if you want to explicitly set the audio state
@ctx.room.local_participant.register_rpc_method("set_audio_output")
async def on_set_audio_output(data: rtc.RpcInvocationData) -> None:
payload = data.payload
if isinstance(payload, str):
try:
payload = eval(payload) # Just a heads up: json.loads is safer for production
except:
logger.warning("Invalid payload format")
return
enabled = bool(payload.get("enabled", True) if isinstance(payload, dict) else payload)
logger.info(f"Setting audio output to {enabled}")
session.output.set_audio_enabled(enabled)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
To sum up, this code helps you:
• Set up a session with RoomOutputOptions.
• Register RPC methods that let you toggle or set _audio_enabled_ directly.
• Use session.output.set_audio_enabled() to control audio output on the fly.
If you have any questions or want me to walk you through more examples, just let me know—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docsbulky-wall-61623
09/08/2025, 8:11 PMtall-airline-16756
09/08/2025, 8:11 PMaclose() method to stop and tidy things up. Like this:
await background_audio.aclose()
Once you run aclose(), the player will stop. If you want to use it again later, you’ll just need to create a new instance.
1. Turn off audio output: You can disable audio output on the fly during your session with session.output.set_audio_enabled(False). Here’s how:
session.output.set_audio_enabled(False)
Put these two steps together, and you’ll have both the BackgroundAudioPlayer and audio output fully switched off. If you want me to walk you through anything else or need more examples, just let me know—I’m here to help!
Sources: Agent speech and audio | LiveKit Docs | Text and transcriptions | LiveKit Docs