give me code to add background audio in livekit ag...
# ask-ai
b
give me code to add background audio in livekit agents python
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you add background audio in LiveKit using the Python SDK! You can use the
BackgroundAudioPlayer
class from the
livekit.agents.voice
module. Here's a simple example to get you started:
Copy code
from livekit.agents import BackgroundAudioPlayer, BuiltinAudioClip, AudioConfig
from livekit.plugins import openai

# Initialize player with ambient and thinking sounds
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),
    ],
)

# Start the player in a room and agent session
await background_audio.start(room=ctx.room, agent_session=session)
If you'd like to use your own custom audio files, no worries! Just provide the file paths like this:
Copy code
import os
background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(
        str(os.path.join(os.path.dirname(__file__), "bg_noise.mp3")),
        volume=1.0,
    ),
)
Just remember to call
start()
after your room connection and agent session have started. If you have any questions or want to explore more about supported audio sources and configurations, please feel free to ask—I’m here to help! Sources: examples/voice_agents/background_audio.py | complex-agents/drive-thru/session_setup.py | Agent speech and audio | LiveKit docs | Agent speech and audio | LiveKit docs