m
hi
t
Thanks for reaching out, Rahib. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! How can I help you today? I'm here to make sure you get the support you need.
m
How to use background audio shouldi create in entrypoint and then pass it to the agent?
t
I'm glad you're interested in using background audio with LiveKit! To get started, you’ll want to create a
BackgroundAudioPlayer
instance right in your
entrypoint
function. You can set it up with some lovely ambient or thinking sounds by using
AudioConfig
objects. Once that’s done, just start it with your room and agent session. Here’s a simple example to guide you:
Copy code
background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)
await background_audio.start(room=ctx.room, agent_session=session)
If you’d like to dive deeper or see a full example, feel free to check out the Background Audio Example on GitHub. I’m here if you have any questions along the way! Sources: Agent speech and audio | LiveKit Docs | examples/drive-thru/drivethru_agent.py
m
no i mean i can create the instance in entrypoint should i like pass it to my agent if i want to use it there?
t
Hi there! Just to clarify, the
BackgroundAudioPlayer
instance you create in the
entrypoint
function doesn’t actually need to be passed to the agent. It works on its own and gets started with the room and agent session using this line:
Copy code
await background_audio.start(room=ctx.room, agent_session=session)
This way, the audio stays perfectly in sync with the agent’s lifecycle. Now, if you ever want your agent to trigger custom audio playback directly, you could set it up to accept the
BackgroundAudioPlayer
, but for most standard uses, that’s not necessary. I hope this helps clear things up! Sources: examples/drive-thru/drivethru_agent.py | Agent speech and audio | LiveKit Docs
m
so how do i play it then in my agent ?
t
I'd be happy to help you play background audio in your agent using the
BackgroundAudioPlayer
. Here's a simple way to get started: 1. First, create the player with your choice of ambient and/or thinking sounds:
Copy code
background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)
2. Next, start the player with the room and agent session like this:
Copy code
await background_audio.start(room=ctx.room, agent_session=session)
3. And if you'd like to play audio on-demand, you can use the
play()
method:
Copy code
await background_audio.play("filepath.ogg", loop=True)
This way, you can add extra audio files or sound effects during your session. If you'd like to see everything put together, feel free to check out the Background Audio example. I'm here if you have any questions along the way! Sources: examples/drive-thru/drivethru_agent.py | docs/index.yaml | Agent speech and audio | LiveKit Docs
m
Is there a way to play background noise if an mcp tool call is made? I want to play like keyboard tapping sounds
t
I'd be happy to help you add some background noise, like keyboard tapping sounds, during an MCP tool call using the
BackgroundAudioPlayer
in Python. This player is great because it automatically syncs with the agent's "thinking" state, which happens during tool calls, including MCP calls. Here's a simple way to get it set up: 1. First, configure the player with a "thinking" sound like this:
Copy code
background_audio = BackgroundAudioPlayer(
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)
2. Then, start the player with the room and agent session:
Copy code
await background_audio.start(room=ctx.room, agent_session=session)
3. Once set up, the "thinking" sound will play automatically during tool execution. If you'd like to play audio manually during specific moments, you can use this:
Copy code
await background_audio.play("path/to/sound.ogg", loop=False)
If you're interested in more customization, like adding verbal prompts before the sound, you can override the
llm_node
function to detect tool calls and trigger extra actions. Just let me know if you'd like me to share an example of that—I’m here to help! Sources: Agent speech and audio | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | agent.py | docs/index.yaml | External data and RAG | LiveKit Docs
m
oh so everytime agent thinks this gets played? isnt this going to cause weird issues ?
t
I completely understand how hearing thinking sounds during every agent "thinking" state might feel a bit much sometimes. This feature is actually designed to make the experience more realistic by filling those quiet moments while the system processes. The
BackgroundAudioPlayer
takes care of playing these sounds only when the agent is in the "thinking" state and stops right after, so there’s no overlapping or extra noise. If this isn’t quite working for you, no worries! You have a couple of options: • You can turn off the thinking sound by setting
thinking_sound=None
when you create the player. • Or, you might prefer using other ways to keep users informed, like verbal prompts (for example, "Let me look that up...") or visual cues in the UI. Rest assured, the system is built to handle these sounds safely, so you won’t run into issues like audio clipping or slowdowns. If you'd like, I’m here to help you tweak this behavior to fit your needs perfectly! Sources: External data and RAG | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs | delay-handling/README.mdoc | Web and mobile frontends | LiveKit Docs