able-jackal-19719
09/09/2025, 6:29 PMtall-airline-16756
09/09/2025, 6:29 PMtall-airline-16756
09/09/2025, 6:30 PMBackgroundAudioPlayer class in the Python SDK! This feature lets you create a list of AudioConfig objects, where each one specifies an audio source, volume, and probability. The system will then randomly pick one audio clip based on those probability values, making your audio experience more dynamic and natural.
Here’s a simple example to get you started:
from livekit.agents import BackgroundAudioPlayer, AudioConfig
# Define background audio player with probabilistic ambient sounds
background_audio = BackgroundAudioPlayer(
ambient_sound=[
AudioConfig("/path/to/ambience_rain.mp3", volume=0.6, probability=0.5),
AudioConfig("/path/to/ambience_cafe.mp3", volume=0.7, probability=0.3),
AudioConfig("/path/to/ambience_fireplace.mp3", volume=0.5, probability=0.2),
]
)
# Start the player in the session
await background_audio.start(room=room, agent_session=session)
In this example, the probabilities control how likely each sound is to play. If the total probability adds up to less than 1.0, you’ll experience natural pauses of silence, which can add to the ambiance. Also, if you want to play sounds on demand, you can use the play() method.
If you’d like to explore more, including supported audio formats and some advanced tips, feel free to check out the Background Audio recipe. I’m here if you have any questions or want to dive deeper!
Sources: examples/voice_agents/background_audio.py | External data and RAG | LiveKit Docs | Agent speech and audio | LiveKit Docs | Agent speech and audio | LiveKit Docsable-jackal-19719
09/09/2025, 6:40 PM2025-09-10 01:38:55,632 - WARNING livekit - AudioMixer: stream <async_generator object BackgroundAudioPlayer._play_task.<locals>._gen_wrapper at 0x1139d7890> timeout, ignoringtall-airline-16756
09/09/2025, 6:40 PMable-jackal-19719
09/09/2025, 6:49 PM