I want to put multiple background audio from my lo...
# ask-ai
a
I want to put multiple background audio from my local files, then put probabilities on them. how to do that?
t
Thanks for reaching out, affandy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're interested in adding multiple background audio files with probabilities using the
BackgroundAudioPlayer
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:
Copy code
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 Docs
a
why I got this warning? and the audio was not played at all
Copy code
2025-09-10 01:38:55,632 - WARNING livekit - AudioMixer: stream <async_generator object BackgroundAudioPlayer._play_task.<locals>._gen_wrapper at 0x1139d7890> timeout, ignoring
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
a
how to make the agent start say the response only after the thinking sound finished?