how to add a sound while the handoff between two a...
# ask-ai
d
how to add a sound while the handoff between two agents
t
Thanks for reaching out, nawaf. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to add a sound during the handoff between two agents in LiveKit, you can easily use the
BackgroundAudioPlayer
to play your own custom audio. Here’s a friendly step-by-step on how to do it: 1. Initialize the BackgroundAudioPlayer:
background_audio = BackgroundAudioPlayer()
2. Start the Background Audio Player:
await background_audio.start(room=ctx.room, agent_session=session)
3. Trigger Sound During Handoff: When an agent initiates a handoff (like with
llm.handoff()
), just play a sound using the
play()
method:
await background_audio.play("handoff_sound.mp3")
4. Use Built-in or Custom Audio Clips: Feel free to use your own file path (like
"handoff_sound.mp3"
) or pick from built-in clips such as
BuiltinAudioClip.OFFICE_AMBIENCE
. Here’s an example:
await background_audio.play(AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8))
5. Integrate with Handoff Logic: Inside your agent’s function or
on_enter()
method, just add the sound playback right before or after calling `llm.handoff()`:
Copy code
@function_tool
async def transfer_to_supervisor(self):
    await self.session.say("Transferring you to a supervisor.")
    await self.background_audio.play("transfer_sound.mp3")
    return llm.handoff(agent=supervisor_agent)
Doing it this way helps keep the user experience smooth and a bit more engaging when switching between agents. Hope this helps! Sources: examples/voice_agents/background_audio.py | README.md
d
give me an example for my own audio
t
Here’s a friendly little guide on how you can use your own audio file during an agent handoff: 1. Get Your Audio File Ready: Make sure your audio file (like
handoff_sound.mp3
) is easy to find in your project folder or hosted somewhere online. 2. Set Up the BackgroundAudioPlayer:
background_audio = BackgroundAudioPlayer()
3. Kick Off the Background Audio Player:
await background_audio.start(room=ctx.room, agent_session=session)
4. Play Your Custom Audio When Handoff Happens: When it’s time to hand off, just use the
play()
method to play your audio file:
Copy code
@function_tool
async def transfer_to_supervisor(self):
    await self.session.say("Transferring you to a supervisor.")
    await self.background_audio.play("handoff_sound.mp3")  # Your custom audio file
    return llm.handoff(agent=supervisor_agent)
Just swap out
handoff_sound.mp3
with the path or URL of your own audio file, and voilà! Your custom sound will play during the handoff.