narrow-engineer-85614
10/05/2025, 6:22 PMtall-airline-16756
10/05/2025, 6:22 PMtall-airline-16756
10/05/2025, 6:22 PMasynccontextmanager. Here’s a friendly example to guide you through it:
import contextlib
from livekit.agents import BackgroundAudioPlayer, BuiltinAudioClip, AudioConfig
@contextlib.asynccontextmanager
async def keyboard_audio_context(room, agent_session, volume=0.8):
"""
Context manager for playing keyboard typing audio during agent thinking.
Automatically starts and stops the background audio player.
"""
player = BackgroundAudioPlayer(
thinking_sound=AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=volume)
)
await player.start(room=room, agent_session=agent_session)
try:
yield player
finally:
await player.aclose()
And here’s how you can use it:
async with keyboard_audio_context(ctx.room, session) as bg_audio:
await agent_session.say("Let me check that for you...")
await asyncio.sleep(5)
This setup will take care of starting and stopping the audio automatically during that block of code. If you have any questions or need a hand with anything else, please don’t hesitate to reach out—I’m here to help!
Sources: examples/voice_agents/background_audio.pynarrow-engineer-85614
10/05/2025, 6:23 PM@asynccontextmanager
async def keyboard_clacking():
"""
Context manager that plays keyboard clacking background audio during the context.
"""
handle: PlayHandle | None = None
background_audio: BackgroundAudioPlayer | None = None
try:
background_audio = BackgroundAudioPlayer()
handle = background_audio.play(audio=AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8), loop=True)
yield
finally:
if handle:
handle.stop()
if background_audio:
await background_audio.aclose()
What's the difference between these two approaches?tall-airline-16756
10/05/2025, 6:24 PMbackground_audio.play() to play audio directly. This is great for on-demand playback of specific audio clips and gives you a PlayHandle for manual control. Just a heads-up, though—`play()` needs start() to have been called first to initialize the audio system. If start() hasn’t been called, play() will raise a RuntimeError.
• My approach: Uses background_audio.start() to initialize the audio system and automatically manage ambient or “thinking” sounds based on the agent’s state. This is especially handy for continuous or event-driven playback since it ties directly into the agent’s lifecycle and doesn’t require you to manually control each audio clip.
In your setup, if play() runs smoothly without calling start() explicitly, it might be because of some implicit initialization happening behind the scenes—but that’s not something we can always count on. My approach aims to make sure the audio system is properly started and cleaned up, which tends to be more reliable when managing background audio during specific parts of a call.
Please feel free to reach out if you want me to explain anything further or help tweak your implementation—I’m here to help!
Sources: examples/drive-thru/drivethru_agent.py | Agent speech and audio | LiveKit Docsnarrow-engineer-85614
10/05/2025, 6:25 PMtall-airline-16756
10/05/2025, 6:26 PMnarrow-engineer-85614
10/05/2025, 6:31 PMnarrow-engineer-85614
10/05/2025, 6:33 PMadd_shutdown_callback to close it?