Give me a sample code how custom background audio ...
# ask-ai
a
Give me a sample code how custom background audio can be set for agent when on thinking state
t
Thanks for reaching out, giri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To set a custom background audio for an agent in the thinking state, you can use the
BackgroundAudioPlayer
class and configure it with a
thinking_sound
parameter. Below is a sample implementation:
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig

# Example using a custom audio file
custom_thinking_sound = AudioConfig(
    source="/path/to/your-custom-audio.mp3",  # Path to your audio file
    volume=0.8,  # Adjust the volume as needed
)

# Initialize BackgroundAudioPlayer with the custom thinking sound
background_audio = BackgroundAudioPlayer(
    thinking_sound=custom_thinking_sound,
)

# Start the background audio player in your entrypoint function
from livekit.agents import AgentSession

async def entrypoint(ctx: JobContext):
    session = AgentSession(
        # Initialize your agent session with other configurations
    )
    await session.start(room=ctx.room)

    # Initialize and start the background audio
    background_audio = BackgroundAudioPlayer(
        thinking_sound=custom_thinking_sound,
    )
    await background_audio.start(room=ctx.room, agent_session=session)
Key Points: • The
source
parameter can be a file path,
BuiltinAudioClip
enum, or an
AsyncIterator
yielding
rtc.AudioFrame
objects. • You can adjust the
volume
parameter (0.0 to 1.0) to control the playback volume. • Ensure the audio file is accessible and in a supported format. If you want to randomize or provide multiple thinking sounds, you can pass a list of
AudioConfig
objects to the
thinking_sound
parameter.