big-microphone-99982
08/28/2025, 8:40 AMtall-airline-16756
08/28/2025, 8:40 AMtall-airline-16756
08/28/2025, 8:41 AMBackgroundAudioPlayer class is a great tool to use. Here’s a simple example to get you started:
from livekit.agents import BackgroundAudioPlayer
# Initialize the player with a file path
background_audio = BackgroundAudioPlayer(
ambient_sound="/path/to/your/audio_file"
)
async def entrypoint(ctx: JobContext):
# Start the player
await background_audio.start(room=ctx.room)
# Play audio on demand
background_audio.play("/path/to/audio_file", loop=True)
# Clean up
await background_audio.aclose()
The BackgroundAudioPlayer works with file paths as strings for both ambient sound and on-demand playback. Just a heads-up: the exact audio formats it supports (like .m4a) aren’t clearly documented, so you might want to try out your file to see how it goes.
Also, if you want to adjust the volume, you can easily do that using AudioConfig like this:
from livekit.agents import AudioConfig
background_audio = BackgroundAudioPlayer(
ambient_sound=AudioConfig("/path/to/audio_file", volume=0.8)
)
If you have any questions or need a hand with anything else, just let me know—I’m here to help!
Sources: examples/voice_agents/background_audio.py | Agent speech and audio | LiveKit Docsbig-microphone-99982
08/28/2025, 8:47 AMtall-airline-16756
08/28/2025, 8:47 AM