red-needle-56026
06/28/2025, 6:31 AMtall-airline-16756
06/28/2025, 6:31 AMtall-airline-16756
06/28/2025, 6:31 AMBackgroundAudioPlayer
class to achieve this. This allows you to add realism by playing ambient background audio alongside the synthesized speech.
Here is an example of how to implement it:
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip
async def entrypoint(ctx: agents.JobContext):
await ctx.connect()
session = AgentSession(
# ... your stt, llm, tts, vad, turn_detection, etc.
)
await session.start(
room=ctx.room,
# ... agent, etc.
)
background_audio = BackgroundAudioPlayer(
# play office ambience sound looping in the background
ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
# play keyboard typing sound when the agent is thinking
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)
The BackgroundAudioPlayer
class supports two types of audio:
• Ambient sound: A looping audio file that plays continuously in the background
• Thinking sound: Audio that plays while the agent is processing/thinking
You can adjust the volume of these sounds using the volume
parameter in the AudioConfig
object, with values between 0 and 1.0.
Sources: Agent speech and audio | LiveKit Docs | GitHub::livekit::agents | examples/voice_agents/background_audio.py