damp-waiter-77572
09/04/2025, 11:05 AMtall-airline-16756
09/04/2025, 11:05 AMtall-airline-16756
09/04/2025, 11:06 AMBackgroundAudioPlayer 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()`:
@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.mddamp-waiter-77572
09/04/2025, 11:07 AMtall-airline-16756
09/04/2025, 11:07 AMhandoff_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:
@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.