How can we add background souds
# ask-ai
r
How can we add background souds
t
Thanks for reaching out, Jean. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to add some background sounds in LiveKit, you can use the
BackgroundAudioPlayer
class available in Python. It’s a handy way to play ambient sounds continuously or even trigger specific sounds during certain actions—pretty neat, right? Using Built-in Sounds: • LiveKit comes with some cool built-in audio clips like
OFFICE_AMBIENCE
and
KEYBOARD_TYPING
. You can easily use these by passing them to the
BackgroundAudioPlayer
.
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip

background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
    ],
)
Using Custom Audio Files: • Got your own audio files? No problem! Just provide the file paths, and they’ll loop automatically if used as ambient sound.
Copy code
background_audio = BackgroundAudioPlayer(
    ambient_sound="/path/to/my-custom-sound.mp3",
)
Starting the Player: • Once you’ve set up your
BackgroundAudioPlayer
, just start it by calling the
start()
method with your room and agent session:
Copy code
await background_audio.start(room=ctx.room, agent_session=session)
If you want to get a bit fancier—like fading volumes or timing things just right—you can create a custom audio handler using
rtc.AudioSource
and
rtc.LocalAudioTrack
. And hey, if you want a full working example to follow along, check out the Background Audio Example. It’s a great resource! Sources: Agent speech and audio | LiveKit Docs | Agent speech and audio | LiveKit Docs | meditation_assistant/README.mdoc | examples/voice_agents/README.md